MEAN RxJS and Reactive Programming — Questions and Answers
Question 1: What does the RxJS `switchMap` operator do when a new value arrives while a previous inner Observable is still active?
- It cancels the previous inner Observable and subscribes to the new one (Correct answer)
- It queues the new value until the previous Observable completes
- It merges both Observables and emits from both concurrently
- It throws an error if a previous Observable is still running
Correct answer: It cancels the previous inner Observable and subscribes to the new one
switchMap cancels (unsubscribes from) any in-flight inner Observable when a new source value arrives, then subscribes to the new inner Observable. This makes it ideal for HTTP search requests where only the latest result matters.
Question 2: Which RxJS creation function converts a JavaScript Promise into an Observable?
- Observable.create()
- from() (Correct answer)
- of()
- defer()
Correct answer: from()
from() can convert various iterable structures into Observables, including Promises. When given a Promise, it wraps it so it emits the resolved value and then completes, or errors if the Promise rejects.
Question 3: What is the key difference between a `Subject` and a `BehaviorSubject` in RxJS?
- A BehaviorSubject requires an initial value and emits it immediately to new subscribers (Correct answer)
- A Subject can be multicasted while a BehaviorSubject cannot
- A BehaviorSubject only allows one subscriber at a time
- A Subject stores all past values while a BehaviorSubject stores only the last one
Correct answer: A BehaviorSubject requires an initial value and emits it immediately to new subscribers
A BehaviorSubject holds and replays its current value to any new subscriber immediately upon subscription. It requires an initial value at construction. A plain Subject emits nothing to late subscribers — they only receive future values.
Question 4: Which RxJS operator should you use to handle errors inside an Observable pipeline without terminating the stream?
- throwError()
- retry()
- catchError() (Correct answer)
- finalize()
Correct answer: catchError()
catchError() intercepts an error in the Observable chain, allowing you to return a fallback Observable (e.g., of([]) or EMPTY) so the stream continues or completes gracefully rather than propagating the error to the subscriber.
Question 5: In Angular, what is the correct way to prevent memory leaks when subscribing to an Observable inside a component?
- Always use the async pipe in templates or unsubscribe in ngOnDestroy (Correct answer)
- Use switchMap on every Observable automatically
- Observables auto-complete when the component is destroyed
- Call observable.complete() after each emission
Correct answer: Always use the async pipe in templates or unsubscribe in ngOnDestroy
Long-lived Observables (like those from services) do not automatically unsubscribe when a component is destroyed. You must either use the async pipe (which handles unsubscription automatically) or manually call unsubscribe() in ngOnDestroy, often via a takeUntil pattern.
Question 6: What does the `combineLatest` operator emit?
- Only when all source Observables have emitted at least one value, then on every subsequent emission from any source (Correct answer)
- Only when all source Observables complete simultaneously
- A single combined value after all source Observables complete
- The first value from whichever source Observable emits first
Correct answer: Only when all source Observables have emitted at least one value, then on every subsequent emission from any source
combineLatest waits until every source Observable has emitted at least one value, then emits an array of the latest values from each source. After that, it re-emits whenever any source emits, using the latest cached value from the others.
What does the RxJS `switchMap` operator do when a new value arrives while a previous inner Observable is still active?