- [Instructor] So far, we've only had one observable
at a time.
What happens if we're dealing with more than one observable?
Well, we have operators for that too.
They're actually many, but this was a bit confusing
for me at first, so I'm going to keep it simple for now
and just show you a few.
We'll start with mergeMap.
First, just for a sec, let's jump back over to the docs.
This is the first time we'll see marvel diagram
with two streams.
See how those two streams are merged into one.
They're actually many different ways to merge two streams,
including combineLatest and with latest from.
For now, we're using mergeMap.
So, for this example, let's create an observable of letters.
We need to import the observable of.
I know all these imports might seem tedious,
but remember we want to keep our bundle size small,
so we're only going to import what we need.
While we're here, let's go ahead
and import the mergeMap as well.
Now, we don't need this, but we're going to keep those numbers
because we're going to use those in a minute.
First, we'll go ahead and define an observable of letters.
And it's just going to be a, b, c, d, and e.
Next, we'll take that letters observable
and we'll mergeMap.
MergeMap expects a function, so we're going to take our payload
and then inside that, we'll go ahead
and call our numbers observable,
which we've already defined.
Then, we'll do our take(5),
because we don't want it to go on forever.
And next, we're going to map just like we did before,
but this time, we'll take our i,
which represents the numbers
and we'll combine it with the letter.
So, in this code, the letters is the x
and the numbers is the i, so we should see in the console,
i plus x.
We'll subscribe to that and just like before,
we're just going to connsole.log our payload.
That's it.
Let's go to the browser and see what this looks like.
So it's still doing the interval of numbers
and now it's adding a letter each time.
So in this way, we can combine two different streams,
we can access the values inside both those streams,
and do whatever we need to, that's mergeMap.