RxJS · mid
Subscription leaks, errors, forkJoin and polling
Know which subscriptions never end, that an error ends an Observable, what forkJoin waits for, and how to poll safely.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- HTTP completes after the response, so there is no lasting leak. But if the component is destroyed first, the callback still runs unless you cancel with
takeUntilDestroyed(). intervalnever completes. It leaks and keeps running after the component is gone.- An error ends an Observable.
catchErrorat the outer level that returnsof([])completes the outer stream, and the search stops working. PutcatchErrorinside the inner Observable. forkJoinwaits for every source to complete, then emits the last values once. One error fails all.- A dashboard stuck on a spinner with no error points to a source that never completes.
- A source that completes without a value makes
forkJoincomplete with nothing. - Polling:
timer(0, 3000).pipe(exhaustMap(() => api.status(id)), takeWhile(s => s.state === "PENDING", true), takeUntil(timer(120_000)), takeUntilDestroyed(destroyRef)). takeWhile(..., true)also emits the final value.exhaustMapskips a tick when a call is still running.
The red pen
Common traps: mistake → fix
Mistake: HTTP calls always leak if I do not unsubscribe
Fix: they complete, but they are not cancelled on destroy.
Mistake: catchError at the end of the pipe keeps the stream alive
Fix: it ends the outer stream after the error.
Easy to hard
Questions interviewers ask, easy to hard
- Which of these subscriptions leak?
- Why did search stop working after one failed request?
- What does forkJoin wait for?
- How would you poll a payment status for two minutes?
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.