All quizzesEasy
Observables & HTTP — Series 3
Preview — 3 of 10 questions
Does anything related to numbers$s values get logged or computed before its subscribed to?
javascript
const numbers$ = of(1, 2, 3);
console.log('created');
// no .subscribe() call yetANo — Observables are lazy by default. Nothing inside an Observable's definition actually runs until something calls .subscribe() on it; numbers$ here is just a description of a future stream of values, not yet an active one
BYes — of(1, 2, 3) evaluates immediately when called, the values just aren't delivered anywhere yet
CIt depends on whether the Observable is "hot" or "cold" — of() specifically is always hot
DOnly the first value (1) is computed eagerly; the rest wait for subscription
For a source emitting 5, what gets logged, and in what order?
javascript
source$.pipe(
tap(value => console.log('saw', value)),
map(value => value * 2),
).subscribe(result => console.log('got', result));Asaw 5, then got 10 — tap runs a side effect (here, logging) with the value passing through completely unchanged, before the rest of the pipeline (map) transforms it further
Bgot 10 only — tap is a no-op unless its result is used
Csaw 10, then got 10 — tap always sees the final, fully transformed value regardless of where it sits in the pipe
Dgot 10, then saw 5 — tap runs after the subscriber's callback
What's the practical difference between these two?
javascript
interval(1000).subscribe(n => console.log('interval', n));
timer(3000, 1000).subscribe(n => console.log('timer', n));AThey're identical; timer is just an older, deprecated name for interval
Binterval(1000) emits 0, 1, 2, ... every second, starting immediately (the first emission is after one second). timer(3000, 1000) waits 3000ms before its first emission, then also emits every 1000ms after that — timer lets you control the initial delay separately from the repeat period, which interval doesn't
Cinterval only emits once; timer repeats indefinitely
Dtimer requires an explicit .subscribe() argument specifying how many emissions to allow
Sign up free to play
Answer all 10 questions (7 more), see explanations for every answer, and track your score.