JS Quiz: Hard: Microtasks vs timers output order

What is the exact console output order?

console.log('1');
setTimeout(() => console.log('2'), 0);
Promise.resolve().then(() => console.log('3'));
queueMicrotask(() => console.log('4'));
console.log('5');
  • 1, 5, 3, 4, 2
  • 1, 3, 4, 5, 2
  • 1, 5, 4, 3, 2
  • 1, 2, 5, 3, 4
0 voters

VaultBoy

Not much of a quiz tbh — it’s 1, 5, 3, 4, 2.

1 and 5 are straight sync logs. Then the engine drains microtasks before it touches timers, so the Promise .then (3) runs, then queueMicrotask (4), and the setTimeout(..., 0) (2) comes after that.