advanced
RxJS
Model async streams with observables, operators, subscriptions, cancellation, multicasting, and template async pipes.
RxJS models async data as Observables — lazy streams with operators (`map`, `switchMap`, `debounceTime`, `catchError`). Angular HttpClient returns Observables; templates use `async` pipe to subscribe and unsubscribe automatically.
users$ = this.route.paramMap.pipe(
switchMap((params) => this.api.getUser(params.get('id')!)),
);
`switchMap` cancels prior inner subscriptions — common for search. `shareReplay` multicasts hot streams. Always manage teardown in components without async pipe (`takeUntilDestroyed`).
On interviews: Observable versus Promise, cold versus hot, memory leaks from forgotten subscriptions, and error handling in streams.
Common pitfalls: nested subscribes, missing unsubscribe on long-lived components, and using `subscribe` in services that should return cold Observables.
The trade-off is expressive stream pipelines versus subscription lifecycle discipline.
Checklist:
- async pipe in templates first.
- switchMap for param-driven HTTP.
- Single error handler per stream chain.
- takeUntilDestroyed for manual subs.