RxJS Operators You Will Use Every Day
pipe and Operators
Operators are small functions that change a stream. You chain them inside pipe().
typescript
import { of } from 'rxjs';
import { map, filter, tap } from 'rxjs/operators'; // or: import { map } from 'rxjs'
of(1, 2, 3, 4, 5, 6).pipe(
filter(n => n % 2 === 0), // 2, 4, 6
map(n => n * 10), // 20, 40, 60
tap(n => console.log('step', n)) // side effect, does not change values
).subscribe(console.log);
Most Useful Operators
| Operator | What it does |
|---|---|
map | Transform each value |
filter | Keep values that pass a test |
tap | Do something without changing the value (logging) |
take(n) / takeUntil | Stop after n values or when another stream emits |
debounceTime(ms) | Wait until the user stops typing |
distinctUntilChanged | Ignore repeated identical values |
switchMap | Switch to a new inner observable and cancel the previous one |
catchError | Handle errors and continue with a fallback |
retry(n) | Try again on error |
startWith | Emit a value first |
combineLatest | Combine the latest values of several streams |
forkJoin | Wait for several observables to finish, then emit all results |
finalize | Run code on complete or error (hide a spinner) |
Live Search: the Classic Example
typescript
import { FormControl, ReactiveFormsModule } from '@angular/forms';
import { debounceTime, distinctUntilChanged, switchMap, catchError, of } from 'rxjs';
@Component({
selector: 'app-search',
imports: [ReactiveFormsModule],
template: `
<input [formControl]="query" placeholder="Search products">
@for (p of results(); track p.id) { <p>{{ p.name }}</p> }
`,
})
export class Search {
private http = inject(HttpClient);
query = new FormControl('', { nonNullable: true });
results = toSignal(
this.query.valueChanges.pipe(
debounceTime(300), // wait for a pause in typing
distinctUntilChanged(), // ignore same text
switchMap(text => text.length < 2
? of([])
: this.http.get<Product[]>('/api/products', { params: { q: text } }).pipe(
catchError(() => of([])))), // an error must not kill the stream
),
{ initialValue: [] as Product[] });
}
Why switchMap? If the user types again before the old request finishes,
switchMap cancels the old request. You never show old results over new ones.
Flattening Operators Compared
| Operator | Behavior | Use for |
|---|---|---|
switchMap | Cancel previous, use the newest | Search, route params, autocomplete |
mergeMap | Run all in parallel | Independent actions, order not important |
concatMap | One after another, in order | Saves that must keep order |
exhaustMap | Ignore new while one is running | Login button, prevent double submit |
Combine Streams
typescript
// latest page + latest filter -> reload
const vm$ = combineLatest([page$, filter$]).pipe(
switchMap(([page, filter]) => this.api.load(page, filter)));
// several requests at once
forkJoin({
user: this.http.get<User>('/api/me'),
orders: this.http.get<Order[]>('/api/orders'),
}).subscribe(({ user, orders }) => { /* both ready */ });
Errors and Retry
typescript
this.http.get<Data>(url).pipe(
retry({ count: 2, delay: 1000 }),
catchError(err => {
console.error(err);
return of(null); // fallback value
}),
finalize(() => this.loading.set(false)),
).subscribe(d => this.data.set(d));
Nested subscribe: Do not subscribe inside
subscribe. Use switchMap or another flattening operator.
Conclusion
pipe chains operators. Learn map, filter, debounceTime, switchMap, catchError and combineLatest and you can solve most real tasks such as search boxes and dependent requests.
Quick Summary: switchMap for latest, debounceTime for typing, catchError inside the inner stream.

