All posts

RxJS Operators: A practical guide with real-world use cases

What is Reactive Programming with RxJS?

RxJS (Reactive Extensions for JavaScript) is a library for reactive programming that uses Observables to manage asynchronous sequences of events. In Angular it is natively integrated: every HttpClient call, every FormControl.valueChanges, every Router event is an Observable.

The real power of RxJS lies in its operators: pure functions that transform, filter, combine, and manage data streams. Mastering them makes the difference between a well-structured reactive application and a tangled mess of nested callbacks.


1. map — Transforming Data

Analogy: like Array.map(), but over a continuous stream over time.

Use case: normalize API response

Practical rule: use map whenever you want to transform the shape of the data without changing emission timing.


2. filter — Filtering Emissions

Allows only values that satisfy a boolean predicate.

Use case: process only relevant keyboard events

Practical rule: use filter as early as possible to reduce downstream work.


3. tap — Side Effects Without Modification

tap performs an action on the current value without altering it. Ideal for logging and UI updates.


4. debounceTime + distinctUntilChanged — Optimizing Search

These operators prevent flooding the backend while the user types.


5. switchMap — Cancel Previous Requests

Automatically cancels previous requests when a new emission arrives.


6. mergeMap — Parallel Execution

Executes all requests in parallel without cancellation.


7. concatMap — Sequential Execution

Executes operations one at a time in strict order.


8. forkJoin — Wait for Multiple Requests

Equivalent to Promise.all() in reactive form.


9. combineLatest — Combine Live Streams

Recomputes output whenever any source emits.


10. takeUntil — Prevent Memory Leaks

Standard Angular pattern for automatic subscription cleanup.


11. catchError — Error Handling

Intercepts errors and allows recovery with fallback Observables.


12. shareReplay — Caching and Sharing Streams

Transforms a cold Observable into a shared hot stream with caching.


Summary: When to Use Each Operator

Operator Use when... Typical example
mapTransform data shapeNormalize API response
filterIgnore irrelevant valuesKeyboard events
tapSide effectsLogging, spinner
switchMapOnly latest mattersAutocomplete
mergeMapParallel requestsUploads
concatMapSequential orderSave operations

Mastering these operators means writing cleaner, more efficient Angular code with fewer async bugs.