Higher-Order Observables — Series 3

Preview — 3 of 10 questions

What makes this the recommended shape for a custom, reusable operator?

javascript
function retryWithLog<T>(count: number) {
  return (source: Observable<T>): Observable<T> =>
    source.pipe(
      tap({ error: e => console.log('retrying after', e) }),
      retry(count),
    );
}
AA custom operator is just a function that takes a source Observable and returns a new one — the standard, low-risk way to build one is to compose existing, already-correct operators inside an internal .pipe(...) call (exactly as shown here), rather than hand-rolling subscription/teardown logic, which is easy to get subtly wrong (missed unsubscription, swallowed errors, wrong completion timing)
BIt manually implements subscribe, next, and unsubscribe from scratch, which every custom operator must do
CretryWithLog must be registered globally via Observable.prototype for .pipe() to recognize it
DThis pattern only works for operators with no configuration arguments like count

What is the relationship between switchMap and switchAll() shown here?

javascript
// These two lines are equivalent:
source$.pipe(switchMap(x => makeRequest(x)));
source$.pipe(map(x => makeRequest(x)), switchAll());
AThey're unrelated; this equivalence is a coincidence specific to this example
BswitchAll() is deprecated in favor of switchMap and should never be used directly
CswitchMap is exactly the fusion of two separate steps: map (projecting each source value into an inner Observable) followed by switchAll() (flattening that resulting higher-order Observable-of-Observables using switching behavior). The same relationship holds for mergeMap = map + mergeAll(), and concatMap = map + concatAll()
DswitchAll() requires its own separate import and cannot be used in a .pipe() chain alongside map

Why use animationFrameScheduler here instead of the default (which uses regular timers)?

javascript
interval(0, animationFrameScheduler).pipe(take(60)).subscribe(() => updateAnimation());
AIt schedules each emission to align with the browser's own repaint cycle, via requestAnimationFrame, instead of an arbitrary timer interval — meaning the animation updates happen exactly when the browser is about to paint a new frame, avoiding wasted work between paints and staying in sync with the display's actual refresh rate rather than a fixed millisecond interval that may not match it
BIt has no practical effect; all RxJS schedulers execute at exactly the same rate
CanimationFrameScheduler runs the callback on a separate Web Worker thread
DIt's required for interval() to work inside an Angular component at all

Sign up free to play

Answer all 10 questions (7 more), see explanations for every answer, and track your score.