Skip to the page

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.

The 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().
  • interval never completes. It leaks and keeps running after the component is gone.
  • An error ends an Observable. catchError at the outer level that returns of([]) completes the outer stream, and the search stops working. Put catchError inside the inner Observable.
  • forkJoin waits 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 forkJoin complete 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. exhaustMap skips 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

  1. Which of these subscriptions leak?
  2. Why did search stop working after one failed request?
  3. What does forkJoin wait for?
  4. 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 voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview