RxJS in Angular โ Chapter 4 | switchMap, mergeMap, concatMap โ Observables Inside Observables
๐ Welcome to Chapter 4! Buckle up โ this is the chapter that trips up even experienced developers. But don't worry. By the end, it will make total sense. The question today is: What do you do when...

Source: DEV Community
๐ Welcome to Chapter 4! Buckle up โ this is the chapter that trips up even experienced developers. But don't worry. By the end, it will make total sense. The question today is: What do you do when you have an Observable that produces valuesโฆ and each value needs to trigger ANOTHER Observable? Sounds weird? Let's make it concrete. ๐ค The Problem โ Observable Inside Observable Imagine this scenario: You have a search box. Every time the user types, you want to search the API for results. The user typing = Observable (stream of text values) The API call = another Observable So you have an Observable that needs to trigger another Observable. This is called a higher-order Observable โ an Observable of Observables. // What you WANT to do (but this DOESN'T work properly): searchInput.valueChanges .pipe( map(searchTerm => this.http.get(`/api/search?q=${searchTerm}`)) // โ Now you have an Observable of Observables! ๐ฑ // subscribe would give you Observable objects, not the actual data! ) .s