switchMap

An operator that switches to a new Observable for each value, cancelling the previous one.

What does switchMap do?

switchMap turns each value into a new Observable (often an HTTP call) and cancels the previous one. Perfect for search boxes: only the latest search matters.

results$ = this.searchText$.pipe(
  debounceTime(300),
  switchMap(text => this.http.get<Movie[]>(`/api/movies?q=${text}`)),
);

Level: Freelancer · More Angular terms at this level

Updated