Angular Web Framework RxJS & Observables 3 — Questions and Answers
Question 1: What does `forkJoin` emit when all source Observables complete?
- Each emission from all sources as they arrive
- An array of the last values from each source Observable (Correct answer)
- The first value emitted by any source
- Nothing — it only handles errors
Correct answer: An array of the last values from each source Observable
forkJoin waits for all source Observables to complete, then emits a single array containing each source's final emitted value.
Question 2: Which RxJS operator delays each emission by a specified time but does not drop values?
- throttleTime
- debounceTime
- delay (Correct answer)
- auditTime
Correct answer: delay
delay shifts every emission forward in time by the specified duration without discarding any values.
Question 3: What is the role of `pipe()` in RxJS?
- Creates a new Subject from an existing Observable
- Chains multiple operators together into a single transformation (Correct answer)
- Converts synchronous code into an Observable stream
- Subscribes to an Observable and returns a Subscription
Correct answer: Chains multiple operators together into a single transformation
pipe() composes multiple RxJS operators in sequence, passing each operator's output as input to the next.
Question 4: Which operator would you use to handle errors by retrying the source Observable a fixed number of times?
- retryWhen
- retry (Correct answer)
- catchError
- onErrorResumeNext
Correct answer: retry
retry(n) resubscribes to the source Observable up to n times when it errors before propagating the error.
Question 5: What does `exhaustMap` do that differs from `mergeMap` and `switchMap`?
- It waits for all inner Observables to finish before subscribing to a new one
- It ignores new source values while the current inner Observable is still active (Correct answer)
- It cancels the current inner Observable when a new source value arrives
- It merges all inner Observables simultaneously without any limit
Correct answer: It ignores new source values while the current inner Observable is still active
exhaustMap drops incoming source values entirely while an inner Observable is in progress, preventing overlapping requests.
Question 6: In Angular, which AsyncPipe behavior automatically handles Observable unsubscription?
- It calls unsubscribe() in ngOnInit
- It unsubscribes when the component's template is destroyed (Correct answer)
- It uses takeUntil internally with a destroy Subject
- It converts the Observable to a Promise before subscribing
Correct answer: It unsubscribes when the component's template is destroyed
The AsyncPipe subscribes to an Observable and automatically unsubscribes when the component's view is torn down, preventing memory leaks.
Question 7: Which RxJS operator emits a value from the source only after a specified quiet period with no new emissions?
- throttleTime
- sampleTime
- debounceTime (Correct answer)
- auditTime
Correct answer: debounceTime
debounceTime waits for a silence window after the last emission before forwarding the value, commonly used for search-as-you-type inputs.
What does `forkJoin` emit when all source Observables complete?