The tap
operator is one of the simplest RxJs
operators because it doesn’t change anything to the data coming into your observable:
Why would we need a function that doesn’t do anything? We saw a first very important example that enabled us to use the async
pipe earlier.
At least three important use cases are:
- Debugging complex
RxJs
scenarios usingconsole.log
(as illustrated in the above marble diagram) - Registering side effects as the data changes (that’s what we did in our async pipe scenario)
- Watching for completion of inner observables (when you’re combining several observables into one subscription)
In short, tap
is a way to spy on what is happening inside an observable stream. Any time you want to extract some data/debug/take a look at what’s going through an Observable, tap
is the easiest answer.
You can find examples for these three scenarios in my tutorial: 3 reasons to use the tap
operator.