Skip to the page

RxJS · Trade-off

switchMap vs mergeMap vs concatMap vs exhaustMap

The interviewer asks

Explain switchMap, mergeMap, concatMap and exhaustMap. Which one would you use for a search box, for saving a form on every change, for a login button users double-click, and for uploading ten files?

Then

Follow-ups the interviewer asks next

  1. What does flattening mean here?
  2. switchMap cancelled a request. Did the server still process it?
  3. How do you limit mergeMap to three at a time?
  4. Why is switchMap dangerous for a Pay or Delete button?
  5. Have you seen a bug from picking the wrong one?

The answer

What a strong answer covers

  • All four map each source value to an inner Observable and subscribe to it. They differ when a new value arrives while the previous inner one is still running.
  • switchMap: unsubscribes from the previous inner one (cancels the HTTP call), so the latest wins. Search, typeahead, route params.
  • mergeMap: runs all in parallel, results in any order. mergeMap(fn, 3) limits concurrency, good for uploads.
  • concatMap: queues and runs one at a time, in order. Autosave where order matters.
  • exhaustMap: ignores new values while busy. Login, submit and pay buttons.
  • Cancelling only stops the browser side; a write may still finish on the server. Writes need idempotency keys and exhaustMap or concatMap, never switchMap.

The red pen

Common traps

Avoid:switchMap for saves or payments; mergeMap for search (old results overwrite new ones); "they're all the same".

Practise this out loud

Say your answer out loud to an AI interviewer that follows up, and get a score for it.

Practise by voice

Practise questions like this out loud

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview