Angular RxJS Basics
# CHAPTER 19
Angular RxJS Basics
1. Chapter Introduction
RxJS (Reactive Extensions for JavaScript) is the library that powers Angular's asynchronous operations — HttpClient returns Observables, Router events are Observables, form valueChanges are Observables. RxJS provides a rich set of operators to transform, filter, combine, and control data streams. Understanding RxJS transforms you from an Angular developer to an Angular architect.2. Learning Objectives
- Understand the Observable/Observer pattern.
- Create Observables from scratch.
-
Use common RxJS operators:
map,filter,switchMap,debounceTime.
-
Chain operators using
pipe().
-
Handle errors with
catchError.
3. The Observable Pattern
An Observable is a stream of data over time. Think of it as a water pipe:- The Observable is the pipe.
-
The Observer (your
.subscribe()callback) is the tap/faucet at the end.
- Data events flow through the pipe.
4. Creation Operators
5. The pipe() Method and Operators
Operators are functions applied inside pipe() that transform the stream without modifying the original Observable.
6. Essential Operators for Angular
#### map — Transform each value
#### switchMap — Cancel previous request, make new one (perfect for search)
#### debounceTime — Delay emissions (for search inputs)
#### distinctUntilChanged — Ignore duplicate consecutive values
#### mergeMap — Like switchMap but doesn't cancel previous requests
#### forkJoin — Combine multiple Observables, emit when ALL complete
7. Real-World Example: Live Search
8. Common Mistakes
-
Nested subscribes: Never subscribe inside a subscribe. Use
switchMapormergeMapinstead.
-
Not completing Observables:
interval()andfromEvent()run forever. Always usetakeUntilDestroyed()ortake(N)to limit them.
9. MCQs with Answers
What is an RxJS Observable?
What method starts receiving values from an Observable?
What method is used to chain RxJS operators on an Observable?
Which operator transforms each emitted value into a new value?
Which operator keeps only values that match a condition?
Which operator is ideal for a live search input — cancels the previous HTTP request when a new search term arrives?
What does debounceTime(300) do?
What does distinctUntilChanged() do?
What does forkJoin do?
What is "nested subscribe" and why is it bad?
10. Interview Questions
-
Q: Explain the difference between
switchMap,mergeMap, andconcatMap.
-
Q: Why would you use
forkJoininstead of multiple sequential API calls?
11. Summary
RxJS is the functional, reactive programming backbone of Angular. By mastering a handful of operators —map, filter, switchMap, debounceTime — you can handle virtually any asynchronous scenario in a declarative, readable, and composable way. These patterns significantly outperform nested promises and callbacks.
12. Next Chapter Recommendation
In Chapter 20: Observables and Subjects, we go deeper into RxJS Subjects —Subject, BehaviorSubject, ReplaySubject — and learn how to use the async pipe to elegantly manage subscriptions in templates.