Merge Values in Parallel with RxJS Operator merge

InstructorAndré Staltz

Share this video with your friends

Send Tweet

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.

🚨 Since we are importing interval from RxJS, we don't need to preface our Observables with Rx.Observable. You can no longer .{operator}, you need to .pipe({operator}) instead.

Klemen
~ 7 years ago

Wouldn't the marble diagram for the example given look like this?

--0-01--21-3--24-(3|)

The (24) wouldn't happen synchronously, just very closely together.

André Staltzinstructor
~ 7 years ago

Hi Klemen. Yes that is to some extent correct, but it depends on the underlying scheduler. Schedulers are a concept we don't cover in this course, but in a nutshell they allow you to choose what is "time" and how to run things with time. Here, the Observable.interval() would schedule emissions with a setInterval, and that's why 2 and 4 would not be emitted synchronously. However, if we used the VirtualTimeScheduler, it would just simulate a setInterval, and essentially then it is able to accomplish emissions of 2 and 4 at the "same time" (under its own concept of time).

What matters here is that each character in the ascii marble diagram stands for one "time unit", and what the original diagram meant is that 2 and 4 occur "in the same time unit". --24- isn't correct either because 4 does not occur "one time unit" (in our case, 100ms) ahead of 2.

Klemen
~ 7 years ago

Ah, OK, that makes sense. Thank you!