## This lesson is for PRO members.

Unlock this lesson NOW!

# Combination operator: merge

3:57 RxJS lesson by

In the category of combination operators, concat executes Observables sequentially, while merge executes them in parallel. This lesson will teach you how the merge operator is an OR-style combination operator that blends two or more Observables together.

Get the Code Now
click to level up

In the category of combination operators, concat executes Observables sequentially, while merge executes them in parallel. This lesson will teach you how the merge operator is an OR-style combination operator that blends two or more Observables together.

The concat operator is a combination operator. It combines two or more observables sequentially. For instance, here we had "foo." We combined it with "more" sequentially to produce this output observable that first delivers values from foo and then delivers values from more.

There are other combination operators which work in a different fashion. Suppose we have these two observables, "foo" and "bar," both of them created with interval. Foo ticks every 500 milliseconds. Bar ticks every 300 milliseconds.

We want to combine them together not sequentially but in parallel. That means that the output observable that we will see at the bottom will deliver values from either foo or bar. That happens in parallel. In practice, this operator that we're going to use is going to subscribe to both of them at the same time. It will deliver the values that come from either one of them.

Bar delivered one value here. That's going to come out in the output observable. But also foo delivered a value. We're going to deliver that. Zero from foo. Then we're going to deliver one from bar, two from bar, one from foo, three.

Then you see here two being delivered at the same time as four and complete from bar. This means that we're going to have two and four being delivered sequentially and synchronously in the same event loop. You can see that bar completed.

But we're not actually going to complete the output observable. That's because, since we're running these two in parallel, we still have values that will be delivered from foo. That's why we are able to continue.

Once we get three and completion from foo, we can complete the output observable because we know that of course if you're just delivering values from both of these, and both of these have completed, then you know that the output observable can't really deliver anything else. That's why we're able to complete.

We just want to blend these two observables together. The name of the operator for this is called merge. It really allows you to do just that. It allows you to merge together these two observables by running them in parallel and blending their delivered values. You use it in an API similar to concat.

We do foo.merge, so that's an instance operator with bar. Then you can get this merged observable which executes like this ones we subscribed, so delivers zero, zero, one, two, one, three, two, four, three, done, just like we saw here in our marble diagram.

Also, merge has a static operator version, just like concat had -- had Rx.Observable.merge foo with bar together. It does the same thing as before. That's why you would use merge, is to deliver values from either one of these member observables.

I like to call merge the "or operator" in RxJS because values being delivered on the output observable come from either foo observable or bar observable. Then you can really see the "or" logic happening there. You can also see it happening in practice.

Let's say this would be all the clicks happening on your app. You know that all of the clicks can either come from clicks on the button or clicks on the page. You can really see there the logic happening because you know that all of the clicks are from this, or from that, or from this, or from that.

In general, that's how you should reason about the merge operator. It blends everything in an or style.

Featured course:

#### Get Started with Elasticsearch

HEY, QUICK QUESTION!
Why are we asking?