Use case of Observable .do() operator (rxjs)

Update

Now it's pipe( tap(...), ) instead of do()

Original

.do() is to execute code for each event. A difference to .map() is, that the return value of .do() is ignored and doesn't change what value the subscriber receives.


Now it's pipe( tap(...), ) instead of do()

const source = of(1, 2, 3, 4);
source.pipe(
  tap(val => console.log('I am tap: ',val)),
  filter(val =>  val > 2),
  map(val => val + 1)).subscribe((val) => {
  console.log('I am subscriber value after filtering: ', val);
});

Output:

I am tap:  1
I am tap:  2
I am tap:  3
I am subscriber value after filtering:  4
I am tap:  4
I am subscriber value after filtering:  5

*Tap operator doesn't modify anything, you can say that it is just to view the stream.