portfolio / notes / rxjs-mapping-operators.mdoc
switchMap, concatMap, mergeMap or exhaustMap?
One new event, four possible choices. The same experiment through the four RxJS mapping operators: each projects values into inner streams and adds its own concurrency policy.
- RxJS
- Angular

What should happen when another request arrives before the current one finishes? The four higher-order mapping operators answer that question in four different ways:
- switchMap: switch to the latest.
- concatMap: queue it and keep the order.
- mergeMap: let operations overlap.
- exhaustMap: ignore it while busy.
Same inputs, same durations. Only the operator changes.
The experiment
Three events, three inner streams. Each event can start a cold request Observable; the operator decides when, or whether, it is subscribed.
| Event | Arrives at | Request takes |
|---|---|---|
| A | 0s | 5s |
| B | 1s | 3s |
| C | 2s | 1s |
Each request emits one response, then completes. There are no errors in this example, and every timeline uses the same scale.
events$.pipe(operator((id) => request$(id)));switchMap: follow the latest
A new event unsubscribes the previous inner stream.
A stops at 1s, B stops at 2s, and only C responds, at 3s. Use it for search results that must follow the latest query. Unsubscribing does not undo server-side changes.
concatMap: wait your turn
New values wait in a queue; each starts after the previous one completes.
A responds at 5s, then B at 8s, then C at 9s. Use it for ordered saves that must all run. If an inner stream never completes, the queue stalls.
mergeMap: let work overlap
Subscribe as events arrive and forward responses as they arrive.
Responses arrive C, B, A: input order is not preserved. Use it for independent requests that can overlap. Concurrency is unlimited by default; pass a limit if you need one.
exhaustMap: busy? Ignore it
New events are ignored until the active inner stream completes.
Only A starts. B and C are dropped, not queued. After A completes, a future event can start a new request. Use it to ignore repeated clicks while a form is submitting.
That is a rule for the interface, not a guarantee. Click A starts a payment; click B, while A is still running, is ignored; A finishes; click C starts a second payment. And a network retry, a second tab or another device never passes through this operator at all. If an operation must happen only once, like a charge, the server has to make it idempotent: the client sends a key that identifies the operation, and a repeated request with the same key returns the first result instead of charging again.
Choose the behaviour
| What should a new event do? | Operator | Responses |
|---|---|---|
| Replace the current work | switchMap | C at 3s |
| Wait and preserve order | concatMap | A 5s, B 8s, C 9s |
| Run alongside current work | mergeMap | C 3s, B 4s, A 5s |
| Be ignored while busy | exhaustMap | A at 5s |
The operator encodes your concurrency policy: choose it from the behaviour your UI needs. These are RxJS operators, also used inside NgRx effects.
Check yourself
Pick an answer to see whether it is right, and why.
Sources: the RxJS API for switchMap, concatMap, mergeMap and exhaustMap, and Angular's HTTP client guide.