In this article I’ll introduce the switchMap() operator. The previous articles in this series include:
1. Basic Terms
2. Operators map, filter, and reduce
3. Using Observable.create()
4. Using RxJS Subject
5. The flatMap operator
In the previous article of this series, I demoed the use of flatMap(). While flatMap() unwraps and merges all the values emitted by the outer observable, the switchMap() operator handles the values from the outer observable but cancels the inner subscription being processed if the outer observable emits a new value. The switchMap() operator is easier to explain with the help of its marble diagram shown next.
The outer observable emits the red circle, and switchMap() emits the item from the inner observable (red diamond and square) into the output stream. The red circle was processed without any interruptions because the green circle was emitted after the inner observable finished processing.
The situation is different with the green circle. The switchMap() managed to unwrap and emit the green diamond, but the blue circle arrived before the green square was processed. So the subscription to the green inner observable was cancelled, and the green square was never emitted into the output stream. In other words, the switchMap() operator switched from processing of the green inner observable to the blue one.
The following example has two observables. The outer observable uses the function interval() and emits a sequential number every second. With the help of the take() operator, we limit the emission to two values: 0 and 1. Each of these values is given to the switchMap() operator, and the inner observable emits three numbers with the interval of 400 milliseconds.
let outer$ = Rx.Observable .interval(1000) .take(2); let combined$ = outer$.switchMap((x) => { return Rx.Observable .interval(400) .take(3) .map(y => `outer ${x}: inner ${y}`) }); combined$.subscribe(result => console.log(`${result}`));
The output of this script is shown next:
outer 0: inner 0 outer 0: inner 1 outer 1: inner 0 outer 1: inner 1 outer 1: inner 2
Note that the first inner observable didn’t emit its third value 2. Here’s the timeline:
1. The outer observable emits zero and the inner emits zero 400 milliseconds later
2. In 800 milliseconds later, the inner observable emits 1
3. In 1000 milliseconds the outer observable emits 1, and inner observable was unsubscribed
4. The three inner emissions for the second outer value went uninterrupted because it didn’t emit any new values
If you replace switchMap() with flatMap(), the inner observable will emit three values for each outer value as shown below.
outer 0: inner 0 outer 0: inner 1 outer 0: inner 2 outer 1: inner 0 outer 1: inner 1 outer 1: inner 2
NOTE: To see it in CodePen, follow this link.
The chances are slim that you’ll be writing outer and inner observables emitting integers but there are various practical use cases for switchMap(). For example, in my Angular apps (Angular comes with RxJS) I use switchMap() with the HttpClient object (it returns observable) to discard the results of the unwanted HTTP requests. Just think of a user that types in an HTML input field (the outer observable) and the HTTP requests are being made (inner observable) on each keyup event. The circles on the diagram are the three characters that the user is typing. The inner observables are HTTP requests issued for each character. If the user entered the third character but the HTTP request for the second one is still pending, the inner observable for the second character gets cancelled and discarded so the browser will never recieve the HTTP response.
TIP. The function interval() is handy if you want to invoke another function periodically based on the specified time interval. For example, myObservable.interval(1000).subscribe(n => doSometing()) will result in calling the function doSomething() every second.
NOTE: If your code has nested subscribe() calls, this should be a red flag to you. Consider re-writing this code using flatMap(), switchMap(), or concatMap().
If you have an account at O’Reilly’s safaribooksonline.com, you can watch my video course “RxJS Essentials” there.
In the next article, I’ll show how to intercept errors from an observable stream with the catch(operator.)
Note that canceling a previous searchHeroes() Observable doesn’t actually abort a pending HTTP request. Unwanted results are simply discarded before they reach your application code.
From https://angular.io/tutorial/toh-pt6#chaining-rxjs-operators
Agree, will replace cancelling HTTP with discarding observable. Thanks.
“If you replace flatMap() with switchMap()”
Maybe, there is an error? Maybe, flatMap and switchMap must change their places?
(sorry for awful english language level.)
Thank you, Anton! Fixed.