Common RxJS operators in Angular

Intermediate · 15 min

map, switchMap, and debounceTime through a real Angular use case: a typeahead search box.

Before thisObservable fundamentals for Angular

Dev Academy · 15 min

Common RxJS operators in Angular

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Dev AcademyReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • `.pipe()` chains operators together, each one transforming the stream and passing its result to the next — the same left-to-right composition idea pipes used on a single value.
  • `debounceTime` exists to stop firing on every keystroke; `switchMap` exists to cancel a previous in-flight request when a new one starts — together they're the textbook typeahead pattern for a reason.
  • `switchMap` cancels the previous inner observable outright; `mergeMap` runs every inner observable concurrently instead — picking the wrong one silently changes your app's behavior under rapid input.

Operators compose a stream the same way pipes compose a value

The built-in-pipes lesson showed {{ value | pipeA | pipeB }} feeding one transformation’s output into the next. RxJS operators do the identical job, just for a stream instead of a single template value, chained through .pipe():

searchTerm$.pipe(
  debounceTime(300),
  distinctUntilChanged(),
  switchMap((term) => this.api.search(term)),
);

Read it top to bottom: the raw stream of search terms goes through debounceTime, then distinctUntilChanged, then switchMap, each operator receiving whatever the previous one emitted and passing along its own transformed result. This is the single most common real-world shape you’ll write with RxJS in Angular — the typeahead search box — and it’s worth understanding each operator’s specific job rather than treating the whole chain as a memorized incantation.

Why each operator in the typeahead chain earns its place

  • debounceTime(300) — waits for a pause in emissions before letting one through. Without it, every single keystroke would fire a search request; users type faster than any API should be asked to keep up with, and most of those intermediate keystrokes represent a search term the user never actually meant to submit.
  • distinctUntilChanged() — skips an emission if it’s identical to the previous one. Catches the case where debouncing still lets through a term that, after all that waiting, turns out to be unchanged — no point re-issuing an identical search.
  • switchMap((term) => this.api.search(term)) — this is the one doing the real work, and the one most worth sitting with. For each search term, it starts a new inner observable (the actual search request) — and if a new term arrives before the previous request finishes, it cancels that previous request and switches entirely to the new one.

Why switchMap specifically, and not mergeMap

This last point is where a subtle, genuinely dangerous bug lives if you reach for the wrong flattening operator. Picture a user typing “cat”, then quickly “cats”. Two requests fire: one for “cat”, one for “cats”. If the “cat” request happens to be slower and resolves after the “cats” request, a naively implemented search (or one using mergeMap, which lets every inner observable run concurrently to completion) could display the “cat” results last, overwriting the correct “cats” results with stale ones. switchMap prevents this outright — the moment the “cats” request starts, the “cat” request is cancelled, so there’s no slower, stale response left to arrive and clobber anything.

This is the core reason switchMap is the default recommendation for “the user typed something new, I only care about the latest” scenarios, while mergeMap is reserved for cases where you genuinely want every request to complete independently (firing off several unrelated uploads, say, where cancelling an earlier one would be wrong).

This lesson’s video builds a full typeahead search example with this exact operator chain — the linked “deeper” video is a dedicated, side-by-side comparison of mergeMap/concatMap/switchMap against the same kind of API call, worth watching once the typeahead example above makes sense, to see the same input produce three genuinely different behaviors depending on which operator flattens it.

Check yourself

  1. 01In a search box wired up with `searchTerm$.pipe(debounceTime(300), switchMap(term => this.api.search(term)))`, what problem does switchMap specifically solve that debounceTime alone wouldn't?

    If the user keeps typing while a previous search request is still in flight, switchMap cancels that stale request and switches to the new one — without it, a slow earlier response could arrive after a later, faster one and overwrite the correct result with stale data. debounceTime only reduces how often requests fire; it does nothing about requests that are already in flight when a newer one starts.

  2. 02Why would using mergeMap instead of switchMap in that same typeahead example be a bug, not just a style choice?

    mergeMap lets every inner observable run to completion concurrently, instead of cancelling the previous one — so if an earlier, slower search response arrives after a later one, it can overwrite the box with outdated results. switchMap's cancellation behavior is what guarantees only the most recent search's response ever reaches the template.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark