Skip to main content

Micro vs Macro tasks

In short

Microtasks and macrotasks are different "task queues" that are processed at different times inside the Event Loop.

  • Macrotasks are "large" events: timers, network requests, DOM events.
  • Microtasks are "instant" tasks that run right after the current code, but before the next macrotask.

Comparison table

CriterionMacrotasks (Task Queue)Microtasks (Microtask Queue)
When they runAfter the stack and all microtasks are clearedRight after the current code (before a macrotask)
QueueTask QueueMicrotask Queue
ExamplessetTimeout, setInterval, setImmediate, event handlers, fetch, XHRPromise.then, Promise.catch, queueMicrotask, MutationObserver
Prioritylowerhigher
Number run at onceOne per Event Loop iterationAll microtasks in a row, until the queue is empty
SourceExternal browser APIs (Web API)Built-in microtasks of the JS engine
Typical purposeDeferred execution, asynchronous eventsReactions to promise settlement, small chains of actions

An example showing the difference

javascript
console.log('1'); setTimeout(() => console.log('2'), 0); Promise.resolve().then(() => console.log('3')); console.log('4');

Output order:

javascript
1 4 3 2

Explanation:

  1. 1 and 4 -> run synchronously (on the stack).
  2. Promise.then(...) -> goes into the microtask queue.
  3. setTimeout(...) -> goes into the macrotask queue.
  4. After the stack clears, all microtasks run -> printing 3.
  5. Then the first macrotask runs -> printing 2.

An important detail

After every macrotask, the Event Loop:

  1. Runs all microtasks (until the queue is empty),
  2. Then takes the next macrotask,
  3. Repeats the cycle.

Visually

javascript
┌─────────────────────────────────────────────────────┐ │ Function calls (Call Stack) │ │─────────────────────────────────────────────────────│ │ Microtasks (Microtask Queue) │ ← run right after the stack │─────────────────────────────────────────────────────│ │ Macrotasks (Task Queue) │ ← run after the microtasks └─────────────────────────────────────────────────────┘

Summary

WhatWhenExample
MicrotaskAfter the current code, before macrotasksPromise.then, queueMicrotask
MacrotaskAfter all microtaskssetTimeout, setInterval, fetch
Event LoopEnsures the stack is empty -> microtasks -> macrotask -> repeat-

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.