🅰️ Angular Intermediate

What is the difference between switchMap, mergeMap, concatMap, and exhaustMap?

Why Interviewers Ask This

This question targets practical, hands-on experience with Angular. Interviewers want to see if you've worked with these concepts in real projects, not just read about them. Strong answers include concrete examples.

Answer

These are higher-order mapping operators in RxJS that transform a value into an Observable and subscribe to it. They differ in how they handle multiple concurrent inner Observables: switchMap: when a new outer value arrives, cancels the current inner Observable and switches to a new one. Use when only the latest value matters (autocomplete search — cancel previous searches when user types again). searchTerm$.pipe(switchMap(term => this.http.get("/search?q=" + term))). mergeMap (flatMap): allows multiple inner Observables to run concurrently. Does not cancel — all complete eventually. Use when order doesn't matter and concurrency is desired (upload multiple files simultaneously). Risk: order of results is non-deterministic. concatMap: queues inner Observables — each new one starts only after the previous completes. Preserves order. Use when order matters and operations must be sequential (process tasks in order, save operations that depend on previous). Slowest — no parallelism. exhaustMap: ignores new outer values while an inner Observable is active. Use to prevent duplicate submissions (form submit button — ignore clicks while request is in flight). submitClick$.pipe(exhaustMap(() => this.http.post("/api/submit", data))). Memory aid: switch = cancel latest; merge = parallel; concat = sequential queue; exhaust = ignore while busy.

Pro Tip

This topic has Angular-specific nuances that differ from general programming. Highlighting those nuances in your answer shows expertise rather than generic knowledge.