Skip to content
tezvyn:

Explain the difference between microtask and macrotask queues

Source: developer.mozilla.orgMediumHow cards are made

Explain the difference between microtask and macrotask queues
Summary

Your event loop mental model and starvation hazards.

Key points

Macrotasks like setTimeout run one per tick; microtasks like Promises drain fully after each task before rendering.

What's really being asked

The interviewer wants to see if you treat the event loop as a priority scheduler rather than a single queue. Seniors should explain that the event loop drives concurrency by interleaving tasks with rendering and microtasks, and they should surface the starvation risk inherent in microtask queues.

The full answer

First, define a macrotask as work scheduled by the browser on the task queue, giving setTimeout or setInterval as concrete examples, and note that user events and script execution also create macrotasks. Second, define a microtask as a callback scheduled to run after the current script or task finishes and the execution stack is empty, giving Promise resolution and queueMicrotask as concrete examples. Third, explain execution order: the event loop picks one macrotask from the task queue, runs it to completion, then drains the entire microtask queue before it considers rendering or fetching the next macrotask. Fourth, mention that because microtasks can enqueue more microtasks, a deep or recursive chain can delay macrotasks and rendering, which is a real performance hazard.

The mistakes people make

A red flag is claiming that setTimeout zero always runs before a Promise.then because it was queued first. Another is describing the event loop as a single queue where everything lines up in arrival order. Some candidates also confuse microtasks with synchronous code or forget that MutationObserver uses microtasks. Finally, omitting the starvation risk suggests you have not debugged real UI jank caused by promise chains.

What usually comes next

The interviewer may ask what happens if a microtask recursively queues another microtask, or how requestAnimationFrame relates to the task and microtask timing. They might also ask you to trace a code snippet mixing setTimeout, Promise, and await, or ask how to yield control back to the browser without waiting a full macrotask tick.

A concrete example

Imagine a button click handler that calls setTimeout zero and then resolves a Promise. The setTimeout callback goes to the macrotask queue, while the Promise.then goes to the microtask queue. After the click handler finishes, the event loop executes the Promise.then first because it drains the microtask queue before moving to the next macrotask. Only after all microtasks finish does the setTimeout callback run. If that Promise.then queues another microtask, that new microtask also runs before the setTimeout.

Interview question

A macrotask calls setTimeout(fn, 0), resolves a Promise, and that Promise's .then() queues another microtask. What is true about execution order after the macrotask ends?

  • a.The first microtask runs, then the setTimeout callback, then the second microtask
  • b.The setTimeout callback runs first because it was queued first
  • c.The event loop interleaves microtasks and macrotasks to prevent blocking
  • d.Both microtasks complete before the setTimeout callback executesCorrect
Why?

After a macrotask finishes, the event loop drains the entire microtask queue before fetching the next macrotask, so recursively queued microtasks still run before the setTimeout callback. Option B is a common misconception about arrival order, and B incorrectly assumes the event loop alternates between queues.

Just read this? Test yourself on what you have been reading.

Read the original → developer.mozilla.org

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles