RxJS · Trade-off
switchMap vs mergeMap vs concatMap vs exhaustMap
Practise this out loud
Say your answer out loud to an AI interviewer that follows up, and get a score for it.
Practise by voiceThe 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
- What does flattening mean here?
- switchMap cancelled a request. Did the server still process it?
- How do you limit mergeMap to three at a time?
- Why is switchMap dangerous for a Pay or Delete button?
- 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
exhaustMaporconcatMap, neverswitchMap.
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 voicePractise 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.