Skip to content

Execution Order

Understanding the exact order JavaScript executes code helps you predict behavior and avoid bugs.

console.log('1. Sync');
setTimeout(() => console.log('2. setTimeout'), 0);
Promise.resolve().then(() => console.log('3. Promise'));
queueMicrotask(() => console.log('4. Microtask'));
console.log('5. Sync end');
// Output:
// 1. Sync
// 5. Sync end
// 3. Promise ← microtask queue
// 4. Microtask ← microtask queue
// 2. setTimeout ← macrotask queue
  1. Sync code runs first (call stack)
  2. Microtasks (Promise.then, queueMicrotask) run next
  3. Macrotasks (setTimeout) run last
document.getElementById('button').addEventListener('click', () => {
console.log('Click event');
});
setTimeout(() => console.log('Timeout'), 0);
Promise.resolve().then(() => console.log('Promise'));
// Clicking the button:
// 1. Promise ← microtask
// 2. Timeout ← macrotask
// 3. Click ← macrotask (user events have higher priority)
  • Sync code always completes before async callbacks
  • Microtasks (promises) run before macrotasks (setTimeout)
  • queueMicrotask() adds to the microtask queue
  • Each event loop cycle: 1 macrotask → all microtasks → render