advanced · 24 minute study
The stack must empty before queued work runs
Promise/timer ordering explains why async UI updates and tests sometimes behave differently from the source's visual order.
Useful preparation · 1 prerequisite
Use this as a map, not a lock. If these ideas are familiar, continue.
- One event travels through a tree — Event delegation, nested buttons, keyboard input and game controls all depend on understanding target, currentTarget and propagation.
First, build a mental model
One JavaScript agent runs one job at a time. Hosts schedule tasks; promise reactions use microtasks. At a microtask checkpoint, queued microtasks drain before the next task is selected.
- Synchronous calls finish their current stack before queued callbacks execute.
- Promise reaction handlers are asynchronous even for already-fulfilled promises.
- A zero-delay timer requests later scheduling, not immediate execution.
- The simplified lab models one agent. Browsers also have task sources, rendering opportunities and worker agents.
console.log('A');
setTimeout(() => console.log('timer'), 0);
Promise.resolve().then(() => console.log('microtask'));
console.log('B');
// In the execution lab, await a timer so detached work is observed:
await new Promise(resolve => setTimeout(resolve, 10));- Stack
A and B print during the current synchronous job.
- Microtask
The promise reaction runs after the stack finishes.
- Task
The timer callback runs in a later task.
Production thinking
Take this beyond the example.
Split long work, measure responsiveness and use workers for suitable CPU tasks. Test scheduling-sensitive logic with controlled clocks where possible.
Sources and further reading
Your evidence
unseen is a local learning signal, not a credential or proof of production competence.
Explain the mental model, then explicitly acknowledge it. Opening a page is not evidence.
Supported retries can complete this lesson. Independent evidence and delayed review determine the skill stage.
Next lesson · A promise is an outcome, not a background thread →