RxJS Basics in Angular β For Interviews #5
RxJS is one of the most asked concepts in Angular interviews β especially for form inputs, API calls, and component communication.
Hereβs your pen & paper-friendly note on Basic RxJS Operators like debounceTime, switchMap, forkJoin, etc.
π What is RxJS?
RxJS (Reactive Extensions for JavaScript) is a library for handling asynchronous data using Observables and operators.
Angular uses RxJS in:
- HTTP calls (
HttpClient) - Reactive Forms
- Component communication
- Route events, timers, etc.
π¦ What is an Observable?
An Observable is a data stream you can subscribe to and react when data comes in.
π Common RxJS Operators to Know
1. debounceTime(ms)
Waits for X milliseconds after the last value before emitting.
Use Case: Search input β wait before firing API
searchForm.get('query').valueChanges
.pipe(debounceTime(300))
.subscribe(val => this.search(val));
π§ Waits for 300ms silence before calling search()
2. switchMap()
Cancels previous observable and switches to new one
Use Case: Auto-complete search β cancel old API calls
searchForm.get('query').valueChanges
.pipe(
debounceTime(300),
switchMap(val => this.apiService.search(val))
)
.subscribe(data => this.results = data);
π§ If user types quickly, old API calls are canceled
3. forkJoin([])
Runs multiple observables in parallel, and emits when all complete
Use Case: Fetch user & settings together
forkJoin([
this.api.getUser(),
this.api.getSettings()
]).subscribe(([user, settings]) => {
this.user = user;
this.settings = settings;
});
π§ Use forkJoin() when you want all responses at once
4. map()
Transforms each emitted value
this.api.getUser().pipe(
map(user => user.name.toUpperCase())
).subscribe(name => console.log(name));
5. filter()
Filters emitted values by condition
form.get('age').valueChanges
.pipe(filter(age => age >= 18))
.subscribe(validAge => console.log(validAge));
6. take(1)
Only take the first value and then complete
this.route.params.pipe(take(1)).subscribe(params => {
this.id = params['id'];
});
π§ How to Write a Basic Observable
const obs = new Observable(observer => {
observer.next('Hello');
observer.next('RxJS');
observer.complete();
});
obs.subscribe(val => console.log(val));
π― Real Interview Use-Cases
| Scenario | RxJS Operator |
|---|---|
| Search input with delay | debounceTime |
| Cancel previous API | switchMap |
| Load multiple APIs at once | forkJoin |
| Convert data before use | map |
| Filter unwanted inputs | filter |
| Auto-unsubscribe after one emit | take(1) |
π¬ Common Interview Questions
- Why use
debounceTime?
π To reduce API load on fast-changing inputs. - What's the difference between
mergeMap,switchMap,concatMap?
πswitchMapcancels previous,mergeMapruns in parallel,concatMapqueues. - When to use
forkJoinvscombineLatest?
πforkJoinwaits for all to complete once.
πcombineLatestemits every time any observable emits.
β Summary Table
| Operator | Use For | Key Behavior |
|---|---|---|
debounceTime |
Delay between emits | Ignores fast inputs |
switchMap |
Cancel previous observable | Keeps only latest request |
forkJoin |
Combine multiple calls (once) | Emits once all complete |
map |
Transform values | Works like JS .map() |
filter |
Allow only valid values | Like JS .filter() |
take(1) |
Take only first value | Good for route params, etc. |
Would you like to:
- Practice a real RxJS + Angular API example?
- Learn error handling with
catchError()andretry()?
Let me know whatβs next! You're doing great β¨πβοΈ