0 day streak
javascript / intermediate
Snippet

Sequential Request Handling with concatMap

Intermediate RxJS developers use concatMap to ensure that asynchronous operations are processed in the exact order they were triggered. Unlike switchMap, it does not cancel previous requests, and unlike mergeMap, it waits for the current operation to finish before starting the next one.

snippet.js
javascript
1
2
3
this.userId$.pipe(
concatMap(id => this.http.get(`/api/user/${id}`))
).subscribe(user => this.currentUser = user);
angular
Breakdown
1
this.userId$.pipe(
Starts an observable stream based on user ID emissions.
2
concatMap(id => this.http.get(...))
Queues the HTTP requests sequentially, preventing race conditions in order-dependent logic.