What is the event loop in JavaScript?

IntermediateJavaScript interview questionBy , updated 18 November 2026

Short answer

The event loop is how JavaScript runs asynchronous code on a single thread. Synchronous code runs on the call stack. When the stack is empty, the engine runs every queued microtask (promise callbacks, await continuations, queueMicrotask), the browser may render a frame, and then the loop takes the next task, such as a timer or a click, and repeats. That order is why a promise callback runs before setTimeout(fn, 0).

The parts

  • Call stack: the functions running right now. JavaScript runs one of them at a time, to completion. See the call stack and execution context.
  • Task queue (often called the macrotask queue): callbacks waiting for their turn, such as timers, DOM events, MessageChannel messages and network callbacks. A browser has several task sources and chooses between them, but each turn runs one task.
  • Microtask queue: callbacks that must run as soon as the current code finishes, before anything else: promise reactions (then, catch, finally), the code after an await, queueMicrotask and MutationObserver callbacks.
  • Rendering: between tasks the browser may update the page: requestAnimationFrame callbacks, style, layout and paint.

One turn of the loop

  1. Take the oldest task from a task queue and run it until the call stack is empty.
  2. Run every microtask. Microtasks queued while doing this also run now, so the queue is fully drained.
  3. If it is time for a new frame (about every 16 ms on a 60 Hz screen), run requestAnimationFrame callbacks, then style, layout and paint.
  4. Go back to step 1.

The script itself is the first task, which is why all synchronous code runs before any callback.

Predict the order

This is the classic interview snippet. Work it out before reading the answer.

console.log("1");
setTimeout(() => console.log("2"), 0);
Promise.resolve().then(() => console.log("3"));
(async () => {
  console.log("4");
  await null;
  console.log("5");
})();
console.log("6");

It prints 1 4 6 3 5 2.

  • 1, 4 and 6 are synchronous. An async function runs synchronously until its first await, so 4 prints straight away.
  • The then callback was queued first and the code after await second, so the microtasks print 3 then 5.
  • The timer is a task, so 2 waits for the next turn of the loop, even with a delay of 0.

Practise this pattern in Predict the event loop order of setTimeout, promises, queueMicrotask and await, then the harder nested event loop order question, where timers schedule promises and promises schedule timers.

Microtasks and tasks side by side

Microtasks Tasks
Examples then, catch, finally, code after await, queueMicrotask, MutationObserver setTimeout, setInterval, clicks and other events, MessageChannel, postMessage
How many run per turn All of them, including ones added while draining One
Can rendering happen in between No Yes

Why it matters for real pages

The page can only repaint and respond to input between tasks. A task that runs for 300 ms freezes the page for 300 ms, and the next click waits. That delay is what Interaction to Next Paint (INP) measures.

Microtasks are even stricter, because the loop will not move on until the queue is empty:

function spin() {
  Promise.resolve().then(spin); // queues another microtask forever
}
spin(); // the page never repaints again

To keep a long job from blocking, split it into chunks and give the browser a turn between them: a setTimeout per chunk, scheduler.yield() in browsers that support it, or move the work to a Web Worker, which runs on its own thread with its own event loop.

setTimeout(fn, 0) is not 0 ms

The delay is a minimum, not a promise. The callback still waits for the current task and every microtask, browsers clamp nested timers to at least 4 ms after a few levels, and background tabs throttle timers much more. For work that should happen before the next paint, use requestAnimationFrame. For work that should happen right after the current code, use queueMicrotask.

Node.js is a little different

Node.js runs its loop in phases (timers, I/O callbacks, setImmediate in the check phase and others) and drains process.nextTick callbacks before promise microtasks. The browser rules above still hold for promises versus timers. setTimeout vs setImmediate vs process.nextTick covers the details.

Common follow-up questions

Is JavaScript single-threaded?

Your code runs on one thread per page (or per worker), so two functions never run at the same moment. The browser itself is not single-threaded: networking, timers, parsing and often painting happen elsewhere, and their results come back to your thread as tasks.

Why does a promise callback run before setTimeout with a delay of 0?

Promise callbacks are microtasks, and every microtask runs as soon as the current code finishes. A timer callback is a task, which waits for the next turn of the loop.

What does await do in terms of the event loop?

It pauses the async function and returns to the caller. When the awaited promise settles, the rest of the function is queued as a microtask. Read async/await vs promises for more.

Where do requestAnimationFrame callbacks run?

In the rendering step, just before the browser paints a frame, after the current task and its microtasks. They do not run at all while a tab is hidden.

Can microtasks starve the page?

Yes. A microtask that keeps queuing another one never lets the loop reach rendering or the next task, so the page stops responding. Timers do not have this problem, because only one task runs per turn.

Mistakes to avoid

  • Saying setTimeout(fn, 0) runs immediately, or before pending promise callbacks

  • Forgetting that an async function runs synchronously until its first await

  • Calling JavaScript "multi-threaded" because the browser does work in parallel

  • Doing heavy work in one long task, or in a recursive chain of microtasks, and blocking input

  • Assuming Node.js and browsers order setImmediate, nextTick and timers the same way

Practise it in code

Interviewers rarely stop at the definition. These questions open in an editor with tests.

  1. Predict the event-loop order of setTimeout, promises, queueMicrotask and awaitEasy15 minFree
  2. Predict nested event loop order: promises that schedule timers, timers that schedule promises, and await in loopsMedium25 min
  3. Predict async function timing: return versus return await, thenables, and extra microtask ticksMedium30 min
  4. Predict the log order of await inside forEach, for...of, map with Promise.all, and reduceMedium25 min
  5. Schedule callbacks with MessageChannel so they run before timersMedium35 min
  6. Implement LazyMan() with a chainable task queue: eat, sleep and sleepFirstMedium30 min