I switched a map and you'll never guess what happened next - Pete Darwin, Shai Reznik, Mike Brocchi
Why Stack Overflow recommends it
Stack Overflow users link it when answering or asking about RxJS switchMap, because it explains how switchMap cancels earlier inner observables in a memorable way.
Recommended by 1 people across 1 different months, 2018 to 2021.
What it is
An ng-conf talk by Pete Bacon Darwin, Shai Reznik and Mike Brocchi that explains the RxJS operators map, switch and switchMap through a comedic, staged skit with several Angular community guest actors. It is aimed at developers who keep forgetting what switchMap does and when to use it.
What people said
Strong rec Max K · 2021-04 · Stack Overflow · on “understanding of switchmap in rxjs”
switchMap, like exhaustMap or mergeMap, is used to flatten observables of observables. The special thing about switchMap is, that if your source observable (in your case: from(fun())), emits a new value, the switchMap operator will unsubscribe from the previous inner observable before subscribing to the new one. Therefore you will always only have one inner subscription. This ng-conf talk explains it perfectly: [this video] In your example you use the pipe function to pipe three switchMaps. The value of your observable (in your subscribe method) will always be the last transformation you made in the pipe function (in your case ("Fun3")). For your specific example you could do something like: cons
Cites Rob Monhemius · 2018-10 · Stack Overflow · on “How to cancel a http.post() while it is waiting for a response?”
I think I figured it out. I will try to explain what went wrong first, after that I ll go over my solution. The problem In the question's code there was a problem. Before a new value was emitted, I would have already performed a http-request in the searchTrigger , but not in the searchTriggerEmpty . Therefore searchTrigger could be emitted after searchTriggerEmpty even tough searchTrigger had been initiated first. The solution The solution was to fix the emitting order. To do that the event would have to be emitted before any requests are made. And when a new event is being triggered the previous event should be cancelled ( switchMap -behavior ). Code explained The searchTrigger emits events
Cites John · 2019-02 · Stack Overflow · on “How to implement autocomplete with RxBinding, RxJava, and Retrofit”
Thanks! I tried out something similar, and it does seem to work, but once successful results are returned the whole chain goes dead, so the user can't delete text and start over with a different search. I guess that makes sense that the observer is done when you reach success and why I ended up with 2 subscribes. I need the textbox observer to continue working as long as the page is active. Maybe I can't use switchMap and have to use the switch strategy I learned from this crazy video: [this video] I guess they're not quite the same thing.
Cites Motassem Jalal · 2019-11 · Stack Overflow · on “SwitchMap with DebounceTime”
I'm implementing some search and filtering controls with Angular. I've started using RxJs for this. First I had really bad time trying to understand operators like switchMap and exauhstMap , so I referred to this video [this video] and this article [LINK] . Now I have better understanding of it. But a question popped up on my mind regarding using debounceTime() with switchMap() . Is it a common practice? Why do I need a debounceTime when switchMap will make sure to cancel all previous requests? So if I'm implementing a search box like Google's, would I need to use both?