← Back to JavaScript, page 2

javascript-0037 · Hard senior

What is the output order of code that mixes await, then, queueMicrotask, and setTimeout, and why?

Synchronous code runs first. Then all microtasks run in the order they were queued, including promise callbacks and the continuation after an await. Only then does the next timer callback run. An await on an already resolved value costs one microtask turn, and returning a promise from an async function or a then callback can add extra turns.

Full answer

To predict the order, simulate two queues: microtasks and macrotasks (timers). Run all synchronous lines, then empty the microtask queue completely, then take one timer.

In the snippet below, the lines inside an async function run synchronously up to the first await. At the await, the rest of the function is queued as a microtask for the moment the awaited value is ready. For a value that is already a native promise, or a plain value, that takes one microtask turn.

Promise callbacks join the queue when the promise is already resolved, or later when it resolves. Two chains of then calls advance in lockstep, one step per turn. A step inside one chain is interleaved with the step of the other, based on when each was queued.

An async function that returns a promise adds extra turns, because the returned promise has to be unwrapped. So return somePromise inside an async function resolves later than return value. The then callback returning a promise has the same extra cost.

Finally, the timer callbacks (setTimeout) run after the microtasks queued in the same turn, even with a delay of 0. Timers with a longer delay run later, and two timers with the same delay run in the order they were created.

Interviewers use this question to check that the candidate understands the queue model, not that anyone memorizes turn counts for exotic cases.

javascript
const log = (label) => console.log(label);

async function first() {
  log('first: start');
  await second();
  log('first: after await');
}

async function second() {
  log('second: running');
}

setTimeout(() => log('timeout 0'), 0);

first();

new Promise((resolve) => {
  log('promise executor');
  resolve();
})
  .then(() => log('then 1'))
  .then(() => log('then 2'));

queueMicrotask(() => log('queueMicrotask'));

(async () => {
  const plain = await 5;
  log('await plain value: ' + plain);
  const viaReturn = async () => Promise.resolve('returned promise');
  viaReturn().then(log);
  Promise.resolve('resolved directly').then(log);
})();

log('sync end');

Run with Node.js v24.22.1 as js-37.js; two runs gave identical output. Output: first: start second: running promise executor sync end first: after await then 1 queueMicrotask await plain value: 5 then 2 resolved directly returned promise timeout 0

Key points

  • Run synchronous code first; then drain all microtasks; then one timer.
  • Code before the first await runs synchronously.
  • await queues the continuation as a microtask.
  • Returning a promise from async or then adds extra microtask turns.
  • Timers with the same delay run in creation order.

Common mistakes

  • Treating await as if it blocks the program.
  • Forgetting that code before the first await runs synchronously.
  • Assuming a promise callback and a setTimeout callback with a zero delay are equal in priority.
  • Memorizing turn counts instead of understanding the queue model.

Follow-ups

  • How does the order change if the awaited value is a thenable that is not a native promise?
  • Where would process.nextTick callbacks appear in Node.js?
  • How does the order differ in a browser, where rendering can happen between macrotasks?

Tips

  • Keep two lists on the whiteboard: microtasks and timers. Update them after every line.
  • Say "first await continuation was queued before then 1" so the interviewer hears the reason, not only the answer.
  • If you are unsure about the extra turns for returned promises, say what you know and explain the unwrap step.