What does exhaustMap do?
The last operator from the "big four" RxJS map operators.
In short (as in an interview)
exhaustMap is an RxJS operator that ignores new values until the current inner Observable completes.
It's used to guard against repeated user actions (for example, a double click).
In detail
exhaustMap works like this:
- a value arrives from the source Observable
- an inner Observable is created
- while it runs, all new values are ignored
- once it completes, the next value can be accepted
Example
import { fromEvent } from 'rxjs';
import { exhaustMap } from 'rxjs/operators';
fromEvent(button, 'click').pipe(
exhaustMap(() => this.http.post('/save', data))
).subscribe();If the user clicks:
click click click click
only one HTTP request executes, the rest are ignored.
Visually
source: a----b----c
exhaustMap:
inner a: ----------
b ignored
c ignored
result: ----------When exhaustMap is used
Very typical Angular use cases:
- form submit
- login
- payment
- preventing double-clicks
- guarding against repeated HTTP requests
For example:
this.loginClicks$.pipe(
exhaustMap(() => this.authService.login())
)Difference from other map operators
This is an almost mandatory question at an Angular interview.
| Operator | Behavior |
|---|---|
| switchMap | cancels the previous one |
| concatMap | queue |
| mergeMap | in parallel |
| exhaustMap | ignores new ones |
An important nuance
exhaustMap does not cancel the current operation, it simply ignores new ones.
This is the main difference from switchMap.
A typical follow-up question
Very often asked:
switchMap vs exhaustMap?
Short answer:
- switchMap → cancels the previous one
- exhaustMap → ignores new ones
A real Angular use case
fromEvent(button, 'click').pipe(
exhaustMap(() => this.api.save())
)Protects against:
- double submit
- duplicate requests
- accidental clicks
A mini cheat sheet for map operators
In an interview, you can answer like this:
switchMap - switches concatMap - queue mergeMap - parallel exhaustMap - ignores
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.