Full answer
When each outer value starts an inner Observable (for example an HTTP call), the question is what to do if a new value arrives before the previous inner stream has finished.
switchMap: cancel the old inner stream and switch to the new one. Use it for typeahead search, route-parameter loading, or "latest value wins" cases. Cancelling an HTTP request also avoids showing stale data.mergeMap: start the new one and keep the old ones running. Results can arrive in any order. Use it for independent parallel work, such as deleting many items. Pass a concurrency limit as the second argument when needed.concatMap: wait for the old one to finish, then start the next, so order is kept. Use it when order matters, such as saving edits one after another.exhaustMap: ignore new outer values while an inner stream is running. Use it for "submit" or "login" buttons to stop double clicks.
A queue at a canteen is a useful picture. concatMap is one counter serving in order. mergeMap is many counters open. switchMap is a customer leaving the counter when a new customer comes. exhaustMap is a "counter busy" sign that turns everyone away.
The example below uses fixed delays so that the order of results is predictable: a takes 30 ms, b 20 ms, and c 10 ms.
A frequent mistake is using switchMap for saves. If the user clicks Save twice, the first request is cancelled in the browser, but the server may have already processed it. For writes prefer concatMap or exhaustMap.
import { Observable, concatMap, exhaustMap, from, lastValueFrom, map, mergeMap, switchMap, timer, toArray } from 'rxjs';
const delays: Record<string, number> = { a: 30, b: 20, c: 10 };
// Inner work: takes longer for 'a' than for 'c'.
const work = (id: string): Observable<string> =>
timer(delays[id]).pipe(map(() => `${id} done`));
const source$ = from(['a', 'b', 'c']); // all three arrive at once
console.log('switchMap ', await lastValueFrom(source$.pipe(switchMap(work), toArray())));
console.log('mergeMap ', await lastValueFrom(source$.pipe(mergeMap(work), toArray())));
console.log('concatMap ', await lastValueFrom(source$.pipe(concatMap(work), toArray())));
console.log('exhaustMap', await lastValueFrom(source$.pipe(exhaustMap(work), toArray())));Tested with RxJS 7 under Node 22.18 or newer (`node main.ts`, package.json with `type: module`), run twice with identical output. Results: switchMap gives only `c done`; mergeMap gives `c done`, `b done`, `a done`; concatMap gives `a done`, `b done`, `c done`; exhaustMap gives only `a done`.
Key points
- `switchMap` keeps the latest, `mergeMap` runs in parallel, `concatMap` keeps order, `exhaustMap` ignores while busy.
- Search and navigation: `switchMap`. Ordered writes: `concatMap`. Button spam: `exhaustMap`.
- `mergeMap` accepts a concurrency limit.
- Cancelling in the browser does not undo what the server already received.
Common mistakes
- Using `switchMap` for save or delete calls, where cancelling is not safe.
- Using `mergeMap` for typeahead, which lets an old slow response overwrite a newer one.
- Nesting `subscribe` calls inside each other instead of using a flattening operator.
Follow-ups
- How would you limit `mergeMap` to three requests at a time?
- What happens to an HTTP call when `switchMap` cancels it?
- Which operator would you choose for a form submit that can be clicked twice?
Tips
- Answer with a use case for each, not only the definition.
- State the save-with-switchMap trap. It is a classic senior-level point.
- Use the marble or canteen picture if the interviewer wants an explanation without code.
Evaluation rubric
- Defines each operator's behaviour on overlap correctly.
- Matches each to a realistic use case.
- Knows the cancellation limit on the server.