# Event loop, microtasks and timers

*JavaScript Developer Study Guide · Asynchronous Programming · Last updated October 16, 2026 · https://www.certifyforce.com/study-guide/javascript-developer/asynchronous-programming/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.

- Certification: Salesforce JavaScript Developer
- Exam section: Asynchronous Programming
- Study guide: https://www.certifyforce.com/study-guide/javascript-developer

**Official exam objective:**

> Given a scenario, use event loop and event monitor or determine loop outcomes.

## What the exam expects

- Predict the log order of synchronous code, promise callbacks, `await` continuations, `queueMicrotask` and `setTimeout`/`setInterval` callbacks.
- 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, `setImmediate` and timers, inside and outside an I/O callback.
- Explain why an error thrown in a timer callback escapes the `try...catch` that 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, `await` continuation or `queueMicrotask` callback, 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

| Fact | Detail | Source |
|---|---|---|
| Run-to-completion | A job finishes before any other job runs | [Execution model](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Execution_model) |
| After each task | Microtask queue drained completely, then possible render | [Microtask guide](https://developer.mozilla.org/en-US/docs/Web/API/HTML_DOM_API/Microtask_guide) |
| `queueMicrotask` vs `then` | Same microtask queue, FIFO | [Microtask guide](https://developer.mozilla.org/en-US/docs/Web/API/HTML_DOM_API/Microtask_guide) |
| Promise executor | Runs synchronously inside `new Promise` | [Promise() constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/Promise) |
| At `await` | Control returns to the caller; continuation is a microtask | [await](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await) |
| Timer delay | A minimum threshold, not an exact time | [The Node.js Event Loop](https://nodejs.org/en/learn/asynchronous-work/event-loop-timers-and-nexttick) |
| Node queue order | nextTick, then promise microtasks, then macrotasks | [Understanding setImmediate()](https://nodejs.org/en/learn/asynchronous-work/understanding-setimmediate) |
| Inside an I/O callback | `setImmediate` always before `setTimeout(0)` | [The Node.js Event Loop](https://nodejs.org/en/learn/asynchronous-work/event-loop-timers-and-nexttick) |
| Recursive `nextTick` | Starves I/O | [The Node.js Event Loop](https://nodejs.org/en/learn/asynchronous-work/event-loop-timers-and-nexttick) |

## Check yourself

- 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?

## Sources

- [JavaScript execution model](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Execution_model)
- [Using microtasks with queueMicrotask()](https://developer.mozilla.org/en-US/docs/Web/API/HTML_DOM_API/Microtask_guide)
- [The Node.js Event Loop](https://nodejs.org/en/learn/asynchronous-work/event-loop-timers-and-nexttick)
- [Understanding setImmediate()](https://nodejs.org/en/learn/asynchronous-work/understanding-setimmediate)
- [Understanding process.nextTick()](https://nodejs.org/en/learn/asynchronous-work/understanding-processnexttick)
- [Don't Block the Event Loop](https://nodejs.org/en/learn/asynchronous-work/dont-block-the-event-loop)
- [await](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await)
- [Promise() constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/Promise)
- [Introducing asynchronous JavaScript](https://developer.mozilla.org/en-US/docs/Learn_web_development/Extensions/Async_JS/Introducing)
- [Window: setInterval() method](https://developer.mozilla.org/en-US/docs/Web/API/Window/setInterval)

## About the full unit

The "Core concepts", "Exam traps" and "Worked scenario" sections of this unit, its 18 linked practice questions, and unlimited timed mock exams are included with the JavaScript Developer pack (USD $19): https://www.certifyforce.com/practice-exams
