Demo:  exhaustAll

The exhaustAll operator discards the newly arrived observables (or tasks), if a previous observable is still in progress.

It is useful for the scenarios where the concurrent requests are considered as duplicate or redundant requests which need to be discarded.

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

//Map each source item to this counting task
const countTill3Task = timer(0,1500).pipe(take(4));

//exhaustAll discards new tasks if an earlier task is still in-progress
const exhaustAllOutput = source
                    .pipe(map(v => countTill3Task))
                    .pipe(exhaustAll());
                    .subscribe(v=>console.log(v));
What is happening ?

The exhaust All operator discards the newly arrived observables representing our tasks: If a previous observable is still in progress.

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 3 task.

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

  • As we can observe the operator is discarding the counting tasks for 1 & 2 : Because they arrive when the counting task for 0 is still in progress. Again, similarly, it discards the counting tasks for 4, since counting task for 3 is still in progress. Such features are perticularly useful in the scenarios where concurrent requests are considered duplicate or redundant.
More on : exhaustAll

The exhaustAll accepts a stream of observables as its input. The RxJs provides another a similar but, a more powerful method in - exhaustMap which combines the features of :

  1. Map : (map raw input data) => (observables representing our desired tasks) and
  2. Execute : exhaustAll operator that discards the newly arrived observables, if an earlier observable is still in-progress.

For example, here are two equivalent code using the two:

  const input = timer(0,2000).pipe(take(4));
  const countTill_2_Task = timer(0,1500).pipe(take(3));

  //1. Code using exhaustAll
  const exhaustAllOutput = input
                      .pipe(map(v => countTill_2_Task))
                      .pipe(exhaustAll());

  //2. Equivalent code using exhaustMap
  const exhaustMapOutput = input
                      .pipe(exhaustMap(v => countTill_2_Task));

In Short :

exhaustMap => map + exhaustAll operator.

We will look at more usage of these exhaustMap/exhaustAll under exhaustMap.