Full Byte Developer.

JavaScript promises and the event loop (quick start)

Sync code runs first, promise then and queueMicrotask drain next, then setTimeout. Three Node demos print the order.

Published here , updated · 7 sources

Synchronous code runs first. When the call stack is empty, the runtime drains the microtask queue (promise .then and queueMicrotask callbacks) before the next task, such as a setTimeout callback [1][5][6]. That is why .then prints before setTimeout(..., 0). Three Node demos below print the order. Part 2 covers starvation and Node depth.

Quick start: run sync, then, microtask, and timeout

bash
node <<'JS'
console.log('A sync');
setTimeout(() => console.log('B timeout 0'), 0);
Promise.resolve().then(() => console.log('C then'));
queueMicrotask(() => console.log('D microtask'));
console.log('E sync end');
JS

Captured on this host (node v22.23.5):

text
A sync
E sync end
C then
D microtask
B timeout 0

C prints before D because .then was queued before queueMicrotask in this script. Both still run before the timer. The HTML Standard puts promise jobs in the microtask queue and has setTimeout queue a task, so both microtasks run before the timer [5][6].

A Promise is an object for the eventual result of async work; you attach handlers with .then, .catch, and .finally [2][3].

Draw the drain order once

Demo 1 drain order 1. Call stack A sync E sync end 2. Microtasks C then D microtask 3. Tasks B timeout 0 [1][5][6] printed order: A, E, C, D, B (Node v22.23.5 on this host)

Three boxes match Demo 1 on Node: the call stack prints A sync and E sync end, microtasks print C then and D microtask, then tasks print B timeout 0. That drain order is why setTimeout zero still waits for promise handlers.

See await resume after sync callers

bash
node <<'JS'
async function main() {
  console.log('1');
  await null;
  console.log('2 after await');
}
main();
console.log('3 sync after main() call');
Promise.resolve().then(() => console.log('4 then'));
JS

Captured on this host:

text
1
3 sync after main() call
2 after await
4 then

main() runs until await, then the caller continues. The lines after await resume with other promise work [1][2].

Check the classic quiz order

bash
node <<'JS'
console.log(1);
setTimeout(() => console.log(2));
Promise.resolve().then(() => console.log(3));
Promise.resolve().then(() => setTimeout(() => console.log(4)));
Promise.resolve().then(() => console.log(5));
setTimeout(() => console.log(6));
console.log(7);
JS

Captured on this host (each number on its own line):

text
1
7
3
5
2
6
4

This quiz comes from javascript.info, which gives the same answer [4]. The timer that prints 4 is created inside a microtask, after the timers for 2 and 6, so it runs last [6].

Browser versus Node for Part 1

The HTML Standard defines the browser order these demos follow [5][6]. Browsers can also update rendering between tasks [5]. Node adds process.nextTick and event loop phases that Part 2 covers [7]. For these three demos, the printed order matches across browser and Node because microtasks still drain before the next timer task [1][5][6].

When you need process.nextTick, starvation demos, and a deeper Node versus browser cut, continue with microtasks, starvation, and Node versus browser. For concurrency vocabulary that maps the same overlap idea into Python and Go, see concurrency vs parallelism in plain words.

Mistakes that fail the interview whiteboard

Saying setTimeout(..., 0) means "run immediately": it still queues a task, so it waits until microtasks drain [5][6].

Treating .then and setTimeout as the same queue: promise jobs are microtasks; timers are tasks [1][5].

Expecting await to resume before the caller finishes the current sync work: Demo 2 prints the sync caller line first [1][2].

Porting Part 1 intuition into Node process.nextTick debates without Part 2: nextTick is a separate, earlier queue in Node's model [7].

Depth: why microtasks exist

MDN's execution model and the HTML Standard both need a place for promise reactions that must run soon after the current script, before the browser paints or the next timer fires [1][5][6]. That place is the microtask queue. queueMicrotask is the explicit enqueue API; .then uses the same queue through promise jobs [3][6].

If you keep scheduling new microtasks from microtasks, you can starve timers. That starvation story, plus Node's process.nextTick, is why Part 2 exists [7]. Part 1 stops once Demo 1 through Demo 3 print the expected order on your machine.

Trace Demo 1 with a pencil

Write five lines on paper before you run the script:

  1. Sync body runs: log A, schedule timer B, queue then C, queue microtask D, log E.
  2. Stack empty.
  3. Drain microtasks in order: C, then D.
  4. Take the next timer task: B.
  5. Final print: A, E, C, D, B.

That pencil trace matches MDN's model and the HTML Standard's microtask checkpoint after a task [1][5][6]. When a teammate insists setTimeout(0) should beat .then, point at step 3. The timer is already scheduled; it is waiting for the checkpoint to finish. Keep Demo 1 on screen while you argue the order so the printout, not a remembered mnemonic, settles the whiteboard.

Promise handlers are still just callbacks

async/await is syntax over the same microtask machinery. Demo 2 already showed the continuation after await lining up after the sync caller finishes [1][2].

Build a one-line interview card

Keep this card on a sticky note:

text
sync stack - microtasks (then / queueMicrotask / await resume) - tasks (setTimeout)

Part 2 adds Node process.nextTick and starvation cases where microtasks never yield [7]. Do not invent a fourth queue name for ordinary .then work.

Practice: predict, then run

Before you paste Demo 3, call the order out loud: 1 7 3 5 2 6 4. Then run it. If your prediction mismatches, re-read which timers were created during the sync phase versus inside a microtask [4][6]. The timer for 4 is late because it did not exist until a microtask created it.

When the printed order matches on your Node version, you have enough for whiteboard interviews that stay inside Part 1 scope. Write the observed order next to the sticky-note card so you can re-check after a Node upgrade without re-deriving the queues from memory. Move to microtasks, starvation, and Node versus browser only when someone asks about process.nextTick or about a page that never paints because microtasks never drain [7].

Capture your Node version next to the printout

Interview notes age poorly without a runtime line. Print the version beside Demo 1:

bash
node -v

Captured on this host. Sandbox capture on the review host; re-run on your box before you treat the numbers as a lab result.

text
v22.23.5

The Part 1 order does not depend on a special Node flag. Still, keep the version in the gist so a later reader can tell whether they are comparing the same runtime family [7]. Browsers follow the HTML Standard for these demos; they may also paint between tasks, which Node does not [5]. Write the version beside the Demo 1 output before you paste the gist into a team chat, so the next person reproduces the same printout instead of arguing about a different runtime [1][7].

A vertical three-layer dark-mode clockwork: a navy stack of synchronous blocks draining first, a tight teal microtask ring of glowing tokens in the middle, and a slower amber macrotask arc at the bottom where delayed tokens arrive last, with no typography or numerals.
Synchronous work finishes first, then microtasks, then the delayed timer queue.

Keep this guide

By Full Byte Developer

Filed under Dev explainers

Questions about this guide

Why does then run before setTimeout(0)?

After the current script finishes, the runtime drains the microtask queue (promise handlers and queueMicrotask callbacks) before it runs the next task, such as a timer. MDN's execution model page and the HTML Standard event loop describe that order.

Is this identical in the browser?

Yes for these three demos. The HTML Standard runs a microtask checkpoint after each task, and setTimeout callbacks are tasks, so browsers print the same order. Browsers can also update rendering between tasks. Node has extra queues such as process.nextTick; that depth is Part 2.

Does await use the microtask queue?

await pauses an async function and resumes through promise handling, so the continuation runs after the current sync work, with other microtasks. Demo 2 shows that order.

Sources

Next in this stack