# 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.

Source: https://fullbytedeveloper.com/blog/javascript-promises-event-loop-quick-start
Published here: 2026-11-16, updated 2026-12-03

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

<figure>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 720 280" role="img" aria-label="Three boxes in drain order: call stack prints A sync and E sync end, microtasks print C then and D microtask, then tasks print B timeout 0, matching Demo 1 on Node.">
  <defs>
    <marker id="jsLoopArrow" markerWidth="10" markerHeight="10" refX="8" refY="3" orient="auto">
      <path d="M0,0 L8,3 L0,6 Z" fill="#14b8a6"/>
    </marker>
  </defs>
  <rect x="0" y="0" width="720" height="280" rx="12" fill="#0b1f3a"/>
  <text x="24" y="36" fill="#f8fafc" font-family="ui-sans-serif, system-ui, sans-serif" font-size="16" font-weight="700">Demo 1 drain order</text>
  <rect x="24" y="56" width="200" height="120" rx="8" fill="#123052" stroke="#14b8a6" stroke-width="2"/>
  <text x="40" y="92" fill="#14b8a6" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">1. Call stack</text>
  <text x="40" y="120" fill="#f8fafc" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">A sync</text>
  <text x="40" y="148" fill="#f8fafc" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">E sync end</text>
  <path d="M224 116 H268" stroke="#14b8a6" stroke-width="3" fill="none" marker-end="url(#jsLoopArrow)"/>
  <rect x="276" y="56" width="200" height="120" rx="8" fill="#123052" stroke="#f59e0b" stroke-width="2"/>
  <text x="292" y="92" fill="#f59e0b" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">2. Microtasks</text>
  <text x="292" y="120" fill="#f8fafc" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">C then</text>
  <text x="292" y="148" fill="#f8fafc" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">D microtask</text>
  <path d="M476 116 H520" stroke="#14b8a6" stroke-width="3" fill="none" marker-end="url(#jsLoopArrow)"/>
  <rect x="528" y="56" width="168" height="120" rx="8" fill="#123052" stroke="#64748b" stroke-width="2"/>
  <text x="544" y="92" fill="#cbd5e1" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">3. Tasks</text>
  <text x="544" y="120" fill="#f8fafc" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">B timeout 0</text>
  <text x="544" y="148" fill="#cbd5e1" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">[1][5][6]</text>
  <rect x="24" y="196" width="672" height="56" rx="8" fill="#123052" stroke="#64748b" stroke-width="2"/>
  <text x="40" y="230" fill="#f8fafc" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">printed order: A, E, C, D, B (Node v22.23.5 on this host)</text>
</svg>
</figure>

*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](/blog/javascript-microtasks-node-vs-browser). For concurrency vocabulary that maps the same overlap idea into Python and Go, see [concurrency vs parallelism in plain words](/blog/concurrency-vs-parallelism-plain).

## 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](/blog/javascript-microtasks-node-vs-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].

## Related reading

[Microtasks, starvation, and Node versus browser](/blog/javascript-microtasks-node-vs-browser), [concurrency vs parallelism in plain words](/blog/concurrency-vs-parallelism-plain), [Python create_task vs await](/blog/python-create-task-vs-await), [what is an API](/blog/what-is-an-api).

Print Demo 1 once on your machine; if the order matches, the microtask rule is doing the work.

Browse the [Dev explainers topic](https://fullbytedeveloper.com/blog/topic/dev-explainers).

Send this to the friend who always misses the setTimeout versus promise quiz.

Image: 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.
https://fullbytedeveloper.com/media/javascript-promises-event-loop-quick-start/javascript-promises-event-loop-quick-start-venice.webp
Synchronous work finishes first, then microtasks, then the delayed timer queue.

## Sources

1. [MDN: JavaScript execution model (event loop), accessed 2026-11-13](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Execution_model)
2. [MDN: Using promises, accessed 2026-11-13](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises)
3. [MDN: Promise, accessed 2026-11-13](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)
4. [javascript.info: Event loop (updated 2024-05-17), accessed 2026-11-13](https://javascript.info/event-loop)
5. [HTML Standard: Event loops and HostEnqueuePromiseJob, accessed 2026-11-13](https://html.spec.whatwg.org/multipage/webappapis.html#event-loops)
6. [HTML Standard: Timers and microtask queuing, accessed 2026-11-13](https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html)
7. [Node.js: The Node.js Event Loop, accessed 2026-11-13](https://nodejs.org/learn/asynchronous-work/event-loop-timers-and-nexttick)
