What Is the JavaScript Event Loop?

The event loop is the mechanism that lets JavaScript, despite running on a single thread, handle asynchronous operations by continuously checking for and pro...

Published November 20, 2026

The event loop is how JavaScript runs asynchronous code on a single thread: it runs the call stack, and whenever the stack is empty it takes the next task from the queues (microtasks first, then macrotasks).

The concept

Synchronous code runs to completion on the call stack. Timers, I/O and events are handled by the environment and queue a callback when ready.

Promise callbacks go to the microtask queue, which empties completely before the next macrotask (setTimeout, events). That is why Promise.then runs before setTimeout(0).

Run order

console.log(1); setTimeout(() => log(2)); Promise.resolve().then(() => log(3)); log(4)
output: 1 4 3 2
sync → microtasks → macrotasks

Practical examples

Ordering

console.log('A')
setTimeout(() => console.log('timeout'), 0)
Promise.resolve().then(() => console.log('promise'))
console.log('B')
// A B promise timeout

Blocking the loop

while (Date.now() < end) {}  // freezes the page and all callbacks

Yield to the loop

await new Promise(r => setTimeout(r))   // let other tasks run

Common mistakes

  • Long synchronous loops that freeze the UI.
  • Assuming setTimeout(0) runs immediately.
  • Unbounded microtask loops starving rendering.

Key takeaways

  • Single thread, task queues.
  • Microtasks before macrotasks.
  • Never block the stack.

FAQ

Is JavaScript multi-threaded?

Not for your code; Web Workers provide separate threads.

Where do async/await fit?

await resumes in a microtask.

More JavaScript articles