Asynchronous Programming13% of the exam
Event loop, microtasks and timers
How the call stack, task queue and microtask queue decide the order of synchronous code, promise callbacks, await continuations, queueMicrotask and setTimeout; why timer delays are minimums and long work blocks the page; how Node.js orders process.nextTick, promises, setImmediate and timers; and why errors thrown in timers escape try...catch.
Given a scenario, use event loop and event monitor or determine loop outcomes.
~6 min read · 18 practice questions ·
What the exam expects
- Predict the log order of synchronous code, promise callbacks,
awaitcontinuations,queueMicrotaskandsetTimeout/setIntervalcallbacks. - Explain why a timer's delay is a minimum, why long synchronous work freezes a page, and how chunking or a Web Worker fixes it.
- Order Node.js callbacks:
process.nextTick, promise microtasks,setImmediateand timers, inside and outside an I/O callback. - Explain why an error thrown in a timer callback escapes the
try...catchthat scheduled it, and use an event monitor to watch events as they fire. - Key terms: call stack (frames of the code running now); task (macrotask: a script, timer callback or dispatched event, run one per loop iteration); microtask (promise reaction,
awaitcontinuation orqueueMicrotaskcallback, run when the stack empties); run-to-completion (a running job is never interrupted); tick (Node's term for one call back into JavaScript).
Key facts
Run-to-completion
- Detail
- A job finishes before any other job runs
After each task
- Detail
- Microtask queue drained completely, then possible render
queueMicrotask vs then- Detail
- Same microtask queue, FIFO
Promise executor
- Detail
- Runs synchronously inside
new Promise
At
await- Detail
- Control returns to the caller; continuation is a microtask
Timer delay
- Detail
- A minimum threshold, not an exact time
Node queue order
- Detail
- nextTick, then promise microtasks, then macrotasks
Inside an I/O callback
- Detail
setImmediatealways beforesetTimeout(0)
Recursive
nextTick- Detail
- Starves I/O
Sources for this section (6)
- Execution modeldeveloper.mozilla.org (opens in a new tab)
- Microtask guidedeveloper.mozilla.org (opens in a new tab)
- Promise() constructordeveloper.mozilla.org (opens in a new tab)
- awaitdeveloper.mozilla.org (opens in a new tab)
- The Node.js Event Loopnodejs.org (opens in a new tab)
- Understanding setImmediate()nodejs.org (opens in a new tab)
- Why does a promise callback queued after `setTimeout(fn, 0)` still log first?
- When does code after `await null` run?
- Why does a 100 ms timer fire late when a long loop is running?
- Which Node API should schedule the next chunk of a long job so requests are still served?
The questions are free to read. Answers with their sources and self-grading are included with the pack.
Keep reading with the JavaScript Developer pack
- Core concepts
- Exam traps
- Worked scenario
The pack includes:
- All 24 lessons of the JavaScript Developer guide, with reading progress
- Interactive decision walks and step-by-step sequence steppers
- Inline practice questions and one-click topic practice (18 questions for this lesson)
- An official source cited for every claim
- An AI tutor that answers from the guide
- Unlimited timed practice exams