JavaScript · The language round

Explain the JavaScript event loop

Hardasyncruntimeevent looppromisesUpdated Oct 2026
Quiz this track

Short answer

The event loop runs one task, then drains the microtask queue (promises), then moves to the next task (timers, I/O), with rendering scheduled between tasks.

JavaScript runs your code on a single call stack, but the browser or Node runtime handles timers, network requests and input in the background. The event loop decides when their callbacks get to run.

The three queues that matter

  • Call stack: synchronous code currently executing
  • Microtask queue: promise reactions, queueMicrotask(), MutationObserver
  • Task queue: setTimeout, setInterval, I/O callbacks, UI events

After every task, the runtime drains the entire microtask queue before taking the next task. That ordering is the point interviewers are testing.

js
console.log("A");

setTimeout(() => console.log("B"), 0);

Promise.resolve().then(() => console.log("C"));

console.log("D");

// A, D, C, B
Sync code runs first, then the promise microtask, then the timer task.

await uses the same microtask queue. Code after an await is a promise reaction, so it resumes before the next timer.

js
async function run() {
  console.log("1");
  await Promise.resolve();
  console.log("2");
}

console.log("0");
run();
console.log("3");

// 0, 1, 3, 2

Why it matters

  • Long synchronous work blocks input, timers and rendering. Split it or move it to a worker.
  • A chain of resolved promises can starve rendering if it never yields to a task.
  • setTimeout(fn, 0) means "next task", not "immediately".
  • requestAnimationFrame runs before paint, as part of the browser's rendering steps.
Node.js adds process.nextTick(), which runs before the promise microtask queue. Browsers do not have it.

Follow-ups you should expect

Interviewers use these to check whether the first answer was understanding or memorisation.

  1. 1What is the difference between a microtask and a macrotask?
  2. 2Where does `requestAnimationFrame` fit?
  3. 3How can you starve the event loop?
  4. 4Is `setTimeout(fn, 0)` really zero milliseconds?
  5. 5How does Node's `process.nextTick()` differ?

Say it in the interview

JavaScript has one call stack. The runtime offloads timers and I/O, and the event loop decides when their callbacks run. After each task it drains all microtasks, which is where promise callbacks and code after await live, before moving to the next task from the timer or I/O queue. So synchronous code runs first, then promises, then timers. Long synchronous work blocks rendering and input, which is why we split heavy work or move it to a worker.

Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.

Related questions

Reading is step one

Now say it out loud.

Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live JavaScript mock interview and get scored feedback on depth, clarity and confidence.