Roadmap › Angular (2 → 21) › Module: Services, DI and HTTP
Lessons in this course (16)

8. RxJS essentials

Intermediate★ Must knowLesson 8 of 16
ts
search = new FormControl("");
results$ = this.search.valueChanges.pipe(
  debounceTime(300),
  map(v => v?.trim() ?? ""),
  distinctUntilChanged(),
  filter(term => term.length >= 2),
  switchMap(term => this.api.search(term).pipe(catchError(() => of([])))),   // cancels the previous request
  shareReplay({ bufferSize: 1, refCount: true }),
);
OperatorUse
map, filter, tapTransform, filter, side effects
switchMapSwitch to a new inner observable, cancel the previous (search, route params)
mergeMapRun inner observables concurrently (fire-and-forget saves)
concatMapRun in order, one at a time (queue of writes)
exhaustMapIgnore new values while busy (login button double-click)
combineLatest, forkJoin, zip, withLatestFromCombine streams (forkJoin = like Promise.all, completes once)
debounceTime, throttleTime, distinctUntilChangedRate control
catchError, retryError handling
takeUntil, takeUntilDestroyed, take(1), firstCompletion/unsubscription
shareReplayMulticast and cache the last value

Subjects: Subject (no initial value), BehaviorSubject (has current value — simple state), ReplaySubject(n), AsyncSubject. Cold observables start per subscriber (HTTP); hot share one source (DOM events, Subjects).

Unsubscribing: the async pipe, takeUntilDestroyed(), toSignal() — avoid manual subscriptions that leak.