Demo:  mergeAll

The mergeAll operator subscribes the incoming observables immediately as they arrive, allowing them to run in parallel.

Hence, the output of these observables may be overlapping with each other as they run in parallel with each other.

const source = timer(0,2000).pipe(take(3));

//Each input is mapped to this countTill2Task.
const countTill2Task = timer(0,1500).pipe(take(3));

//mergeAll runs the incoming observables immediately as it arrives.
const process = source
                    .pipe(map(v => countTill2Task))
                    .pipe(mergeAll());
                    .subscribe(v=>console.log(v))
What is happening ?

The merge All operator subscribes the incoming observables immediately as they arrive, allowing them to run in parallel.

Let's verify how it works using our demo.

  • We are mapping each of our input from the source to an observable which is a simple count till 2 task.

Since we are passing these tasks to the merge All operator as a stream:

  • We can observe that the operator is subscribing each of the observables immediately as they arrive.
  • Allowing us to run all of them in parallel.

For instance:

  • As we can see, the counting of 0, 1 & 2 for each input data 0, 1 and 2 is visible in the output.
  • Moreover, each counting task has started as soon as it's corresponding input has arrived.
  • Lastly, since these observables are running in parallel, the output from them are overlaping with each other.

This is operator is perticularly useful when we have to excute a set of independent tasks and complete them in the best possible time, by running them in parallel.