Full answer
RxJS observables are synchronous unless an operator introduces time. of(1, 2, 3) emits all values immediately during subscribe. A scheduler lets you choose when work happens relative to the browser event loop.
Main schedulers:
queueScheduler: synchronous, but orders nested work in a queue (rarely needed).asapScheduler: schedules work in a microtask (like a Promise callback). It runs after the current code finishes, before timers and before rendering.asyncScheduler: usessetTimeoutorsetInterval(macrotask). Used internally bydelay,timer,interval, anddebounceTime.animationFrameScheduler: usesrequestAnimationFrame, good for animation-like updates.
Operators that use them:
observeOn(scheduler): re-emits each value on the given scheduler. It affects delivery to the subscriber. Use it to move expensive subscriber work off the current call stack, or to batch to the next frame.subscribeOn(scheduler): delays the subscription itself.- Time operators accept a scheduler as the last argument, which is also how marble tests use virtual time (
TestScheduler).
Order of execution in one run: synchronous code first, then microtasks (asap), then macrotasks (async). Example: subscribing with synchronous, asap and async delivery prints "sync", then "asap", then "async", regardless of the order you wrote them.
Practical uses: observeOn(animationFrameScheduler) to avoid updating the DOM more than once per frame; observeOn(asapScheduler) to avoid re-entrancy bugs, where a handler triggers another emission in the middle of the current one; and asyncScheduler in tests with fake timers.
With Angular, schedulers interact with zones and change detection: an asyncScheduler timer is a zone-patched setTimeout and can trigger change detection, and in zoneless apps the update still needs a signal or markForCheck.
import { asapScheduler, asyncScheduler, observeOn, of } from 'rxjs';
console.log('start');
of('sync').subscribe((v) => console.log(v));
of('asap').pipe(observeOn(asapScheduler)).subscribe((v) => console.log(v));
of('async').pipe(observeOn(asyncScheduler)).subscribe((v) => console.log(v));
Promise.resolve().then(() => console.log('promise microtask'));
console.log('end');RxJS 7.8. The script was run in Node 24 twice with identical output, which shows sync first, then asap, then async.
Key points
- Default delivery is synchronous. Schedulers choose microtask, macrotask or animation frame.
- `observeOn` changes delivery to the subscriber. `subscribeOn` changes when subscription starts.
- Order: synchronous, then asap (microtask), then async (macrotask).
- Use `animationFrameScheduler` to limit DOM updates to once per frame.
Common mistakes
- Assuming `of(...)` is asynchronous.
- Using `observeOn(asyncScheduler)` and being surprised when ordering changes.
- Forgetting that timers in the zone can trigger change detection.
Follow-ups
- How does `TestScheduler` use virtual time?
- Why does `asapScheduler` run before `asyncScheduler`?
- What does `debounceTime` use by default?
Tips
- Show the execution order example.
- Name the event loop terms microtask and macrotask correctly.
- Link to tests with virtual time.
Evaluation rubric
- Names the schedulers and what each uses.
- Explains observeOn and subscribeOn.
- Predicts the order of output.