0 day streak
javascript / intermediate
Snippet

Parallel Processing with mergeMap

Unlike switchMap, mergeMap does not cancel previous requests. It handles multiple inner Observables concurrently, making it ideal for fire-and-forget actions or parallel data fetching where order doesn't matter.

snippet.js
javascript
1
2
3
4
5
this.userIds$.pipe(
mergeMap(id => this.userService.getDetails(id))
).subscribe(userDetails => {
this.allUsers.push(userDetails);
});
angular
Breakdown
1
mergeMap(id => ...)
Maps each ID to an HTTP request and executes them all in parallel.
2
subscribe(...)
Receives results as they arrive, regardless of the order they were sent.