Skip to main content

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:

  1. a value arrives from the source Observable
  2. an inner Observable is created
  3. while it runs, all new values are ignored
  4. once it completes, the next value can be accepted

Example

ts
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:

ts
this.loginClicks$.pipe( exhaustMap(() => this.authService.login()) )

Difference from other map operators

This is an almost mandatory question at an Angular interview.

OperatorBehavior
switchMapcancels the previous one
concatMapqueue
mergeMapin parallel
exhaustMapignores 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

ts
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 ready
Premium

A concise answer to help you respond confidently on this topic during an interview.