Using switchMap for Dependent HTTP Requests in RxJS
The switchMap operator is ideal for handling dependent HTTP requests where you need to:
Make an initial request
Use its response to make a subsequent request
Cancel the previous inner observable if a new outer value arrives
Basic Usage
import { switchMap } from 'rxjs/operators';
import { from } from 'rxjs';
firstRequest().pipe(
switchMap(firstResponse => {
return secondRequest(firstResponse.id);
})
).subscribe(secondResponse => {
});