RxJS Crash Course – SwitchMap operators


Now let’s move on to switch map this operator is especially useful when you have one observable that you need a value from before you can get a second observable.

RxJS Crash Course –  SwitchMap operators
SwitchMap operators

In this example, we have an observable of clicks but we actually don’t care about the click data. We want to start an interval timer after each click so on the first click the interval should start. And then when we get a new click it’ll reset the interval back to zero.

So, we’ll go ahead and click the page and then we’ll let the interval count up to 6 and then click again and it’ll reset back to zero.

switchMap is commonly used in app development when you have an observable of say a user ID and then you need that user ID first before querying a database for that user’s data.

Let’s move to the next article: