Angular / ReactiveX - switchMap
Explain the use of RxJS - switchMap the simplest and easiest as possible.
I've searched for some simple examples to learn about RxJS switchMap component, and I've had too much difficult to reach a simple example that could teach me how and when use it.
There're 3 main streams on the example given:
- timer(0,5000)
- interval(1000)
- interval(100) The first stream calls its subscribers each 5 seconds, the second stream calls each 1 second and the last one calls each 0.1 second. Whatever stream, when it calls its subscribers and inside the PIPE there is a switchMap, this subscriber will be cancelled and create another one. That's the reason each 5 seconds the second stream restart counting and the reason each 1 second the last stream restart couting.
Attention: tap
operator is just to do something inside the pipe ( the same as the old operator do
).