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.

3 min read

  • RxJS
  • Angular
Responses of the same three requests under each operator: switchMap emits C at 3s; concatMap A at 5s, B at 8s and C at 9s; mergeMap C at 3s, B at 4s and A at 5s; exhaustMap only A at 5s.

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.

EventArrives atRequest takes
A0s5s
B1s3s
C2s1s

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.

0123456789sInABCABCOutC
Bars are active subscriptions; Out shows the responses.

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.

0123456789sInABCABqueuedCqueuedOutABC
The dotted lines are events waiting in the queue.

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.

0123456789sInABCABCOutCBA
All three requests run at once; responses arrive in the order they finish.

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.

0123456789sInABCABignoredCignoredOutA
B and C are dropped, not queued.

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?OperatorResponses
Replace the current workswitchMapC at 3s
Wait and preserve orderconcatMapA 5s, B 8s, C 9s
Run alongside current workmergeMapC 3s, B 4s, A 5s
Be ignored while busyexhaustMapA 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.

Search as you type: the results must always match the latest query. Which operator replaces ????
query$.pipe(
  debounceTime(300),
  ???((q) => this.api.search(q)),
);

switchMap unsubscribes from the previous request when a new query arrives, so a slow, stale response never overwrites the latest results.

A form creates a resource on every «Save». Each click must create its own resource, in order, and no request may be cancelled or skipped by the next one. Which operator?

concatMap queues each creation and starts it when the previous one completes: all of them run, in order. switchMap could cancel one in flight; exhaustMap would drop it.

A «Pay» button: while a payment is in flight, more clicks must not start another one. Which operator?

exhaustMap drops clicks while the first payment is running. It guards the button, not the charge: a click after it finishes, a retry or another tab can still send a second request, so the server must make the payment idempotent.

With the experiment's events (A takes 5s, B 3s, C 1s), in what order do the responses arrive?
events$.pipe(mergeMap((id) => request$(id)));

mergeMap runs the three requests at once and forwards each response as it finishes: C at 3s, B at 4s, A at 5s. Input order is not preserved.

switchMap cancels a request that already reached the server. What does that undo?

Unsubscribing stops listening; it does not roll back work the server already did. Avoid switchMap for writes that must not be lost.

Sources: the RxJS API for switchMap, concatMap, mergeMap and exhaustMap, and Angular's HTTP client guide.

Have a product in mind? Let’s talk.

An idea on a napkin, a process that slows your team down, a product that needs another pair of hands: tell me about it. I read every message and answer with honest questions, not a sales pitch.