1.What is RxJS?
RxJS stands for Reactive Extensions for JavaScript — a library for working with asynchronous data streams like events, API responses, timers, etc.
📌 Think of a water pipe (stream) where data flows continuously and you can react to it anytime.
Use Cases:
- Search input autocomplete
- Live notifications
- Real time stock price update
2.What is an Observable?
An Observable is a data producer — it emits values over time.
Real-life analogy:
You subscribe to a YouTube channel → new video notifications keep coming.
Example:
const obs$ = new Observable(sub => {
sub.next('Hi!');
sub.next('Hello!');
sub.complete();
});
3.What is an Observer?
An Observer is a consumer — it listens to data from Observable.
obs$.subscribe({
next: value => console.log(value),
error: err => console.log(err),
complete: () => console.log('done!')
});
4.What is Subscription?
A subscription connects an observer to an observable.
Like:
Gym membership → you receive facilities (data).
You unsubscribe → no more facilities.
const sub = obs$.subscribe();
sub.unsubscribe();
5.What is a Subject?
A Subject is both Observable + Observer.
It allows multicasting — emits to multiple subscribers.
Real life:
WhatsApp group message → everyone gets it.
const sub$ = new Subject();
sub$.subscribe(v => console.log('A:', v));
sub$.subscribe(v => console.log('B:', v));
sub$.next('Hi');
6.Subject vs BehaviorSubject?
| Feature | Subject | BehaviorSubject |
| Initial value | ❌ No | ✔️ Yes |
| Last value replay | ❌ No | ✔️ Yes |
Use case:
- Store logged-in user state and let components auto-update.
const user$ = new BehaviorSubject(null);
user$.next({name:'Jignesh'});
7.ReplaySubject vs AsyncSubject?
| Type | Behavior |
| ReplaySubject | Replays N previous values |
| AsyncSubject | Emits only last value after complete |
Use case:
Replay last 3 chat messages when new user joins.
8.What are Operators in RxJS?
Operators are functions that transform, filter, or combine streams.
Types:
- Creation → of(), from(), interval()
- Transformation → map(), switchMap()
- Filtering → filter(), debounceTime()
- Combination → merge(), combineLatest()
- Utility → tap(), finalize()
9.Cold vs Hot Observable?
| Cold | Hot |
| Starts emitting when subscribed | Keeps emitting |
| API calls | WebSocket, Subject |
Example:
Watching a recorded movie vs live IPL match 🎯
10.What is Pipeable Operator?
Operators used via .pipe() for clean chaining.
obs$.pipe(
filter(v => v > 5),
map(v => v * 2)
);
11.What is map() in RxJS?
Transforms each emitted value.
of(1,2,3).pipe(
map(v => v * 10)
);
Use case → Format API response
12.What is switchMap()?
Cancels previous request and subscribes to new one.
Best for:
- Live search — Fast typing cancels old requests
searchInput$.pipe(
switchMap(term => this.api.search(term))
);
13.switchMap vs mergeMap vs concatMap vs exhaustMap
| Operator | Behavior | Best Use |
| switchMap | Cancel old, take new | Search input |
| mergeMap | Run parallel requests | Chat uploads |
| concatMap | Queue (1 by 1) | Form submit logs |
| exhaustMap | Ignore new until current completes | Prevent double click on Pay button |
Fantastic interview question.
14.What is debounceTime()?
Waits for user to stop typing before sending request.
keyup$.pipe(debounceTime(500))
Use case → search, autocomplete
15.What is throttleTime()?
Allows value only every X time.
Use case → Prevent rapid button clicks
16.What is take() / takeUntil()?
Unsubscribe automatically after specific condition.
takeUntil(this.destroy$)
Use case → Prevent memory leaks in Angular
17.What is tap()?
Side-effect operator — used for logging, debugging.
tap(v => console.log('Value:', v))
18.What is shareReplay()?
Shares the same API result with future subscribers.
Use case:
- Avoid multiple API calls → Cache response
19.combineLatest vs forkJoin
| Operator | When values emitted? |
| combineLatest | Whenever any observable emits |
| forkJoin | When all complete (like Promise.all) |
Use case:
- combineLatest → calculate total cart price live
- forkJoin → load page after all API calls finish
20.What is error handling in RxJS?
Use catchError() and optionally retry().
api$.pipe(
retry(2),
catchError(err => of([]))
);
21.What is a Scheduler?
Controls when observable delivers values
(e.g., asyncScheduler → delay in microtasks).
22.What is finalize()?
Runs when observable completes or unsubscribes — cleanup.
finalize(() => loader.hide())
23.What is distinctUntilChanged()?
Prevent duplicate API calls & UI renders.
24.Multicasting in RxJS?
One execution shared with multiple subscribers → using Subject / share()
25.Why RxJS in Angular?
✔ HTTP returns Observable
✔ Forms & events
✔ Router events
✔ State management (NgRx)
26.What is backpressure in RxJS?
Handling too many values emitted too fast
Use → throttleTime(), sampleTime()
27.What is defer()?
Creates observable only when subscribed
Useful for lazy-loaded API calls
28.What is timer() & interval()?
Create streams over time:
timer(2000), interval(1000)
Use case → Auto logout timer
29.How to cancel previous subscription automatically?
Use takeUntil()
this.sub$.pipe(takeUntil(this.destroy$))
30.Most used RxJS operator in Angular HTTP?
switchMap() + catchError() + tap() + shareReplay()
31.How to Convert Promise to Observable?
from(fetch('/api/users'))