JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsjavascript
PrevNext

Learn the concept

Event Loop & Runtime

javascript
mid
runtime

What is the difference between microtasks and macrotasks in JavaScript?

event-loop
microtasks
macrotasks
promises
async
runtime
Quick Answer

Microtasks (Promises, queueMicrotask, MutationObserver) run immediately after the current script and before any macrotasks. Macrotasks (setTimeout, setInterval, I/O) run one per event loop iteration. The microtask queue is fully drained before the next macrotask executes.

Detailed Explanation

The JavaScript event loop processes two types of asynchronous task queues:

Microtasks (higher priority):

  • Promise.then/catch/finally callbacks
  • queueMicrotask()
  • MutationObserver
  • process.nextTick() (Node.js — own queue, runs before Promise microtasks)

Macrotasks (lower priority):

  • setTimeout / setInterval
  • setImmediate() (Node.js)
  • I/O operations
  • UI rendering events

Rendering callbacks (separate from both queues):

  • requestAnimationFrame (runs during rendering phase)

Execution order per event loop cycle:

  1. Execute current synchronous code (call stack)
  2. Drain the entire microtask queue
  3. Execute one macrotask
  4. Drain the microtask queue again
  5. Render UI updates if needed (requestAnimationFrame runs here)
  6. Go to step 3

Key insight: Microtasks can starve macrotasks — if microtasks keep adding more microtasks, setTimeout callbacks will be delayed indefinitely.

Code Examples

Execution order demoJavaScript
console.log('1: Synchronous');

setTimeout(() => {
  console.log('2: Macrotask (setTimeout)');
}, 0);

Promise.resolve().then(() => {
  console.log('3: Microtask (Promise)');
});

queueMicrotask(() => {
  console.log('4: Microtask (queueMicrotask)');
});

console.log('5: Synchronous');

// Output order:
// 1: Synchronous
// 5: Synchronous
// 3: Microtask (Promise)
// 4: Microtask (queueMicrotask)
// 2: Macrotask (setTimeout)

Real-World Applications

Use Cases

UI Update Batching

Using microtasks to batch multiple DOM updates before the browser renders, avoiding unnecessary repaints

Framework Reactivity

Vue.js uses queueMicrotask to batch reactive state changes and apply DOM updates in a single tick

Ensuring Execution Order

Scheduling a callback to run after current synchronous code but before any I/O or rendering via queueMicrotask

Mini Projects

Task Queue Simulator

beginner

Build a visual simulator showing how microtask and macrotask queues are processed by the event loop

Custom Scheduler

intermediate

Build a task scheduler that prioritizes work using microtask and macrotask queues with configurable priorities

Industry Examples

Vue.js

nextTick() uses microtask scheduling to batch DOM updates after reactive state changes within a single tick

MutationObserver

Browser API using microtask timing to observe and react to DOM changes efficiently without polling

Resources

MDN - Using microtasks

docs

JavaScript.info - Event loop: microtasks and macrotasks

article

Related Questions

Explain the JavaScript Event Loop and how it handles asynchronous operations.

mid
runtime

What are Promises in JavaScript and how do they work?

mid
async
Previous
What is the difference between shallow copy and deep copy in JavaScript, and how do you create each?
Next
What are Promise.all, Promise.race, Promise.allSettled, and Promise.any, and when do you use each?
PrevNext