Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Microtasks and Macrotasks in JavaScript

Compare queueMicrotask, promise reactions, and timers in JavaScript. A nested microtask joins the queue's end; all queued microtasks run before the timer.

AdvancedAdvanced JavaScriptExample 12 of 12

Run this example once in a fresh browser console. The console shows the printed results.

microtasks-and-macrotasks.js
1console.log('A: script starts');
2setTimeout(() => console.log('E: timer task'), 0);
3queueMicrotask(() => {
4 console.log('C: first microtask');
5 queueMicrotask(() => console.log('D2: added microtask'));
6});
7Promise.resolve().then(() => console.log('D1: promise reaction'));
8console.log('B: script ends');

Sample Output

A, B, C, D1, D2, E

A and B are synchronous. C appends D2 behind the already-queued D1; the microtask queue empties before timer E.

In this one

  • Trace microtasks in insertion order
  • Compare queueMicrotask and promise reactions
  • Follow a nested microtask
  • Avoid starving ordinary tasks

How it works

  1. 1

    Print A and queue the later timer E.

  2. 2

    Queue C, which will add D2 when it runs.

  3. 3

    Queue the promise reaction D1, then print B.

  4. 4

    Process C; append D2 after the already-waiting D1.

  5. 5

    Drain D1 and D2 before running timer E, matching the six-line output.

Edge cases

Endless microtask chain

A callback can keep adding another microtask forever.

That can prevent timers and rendering from getting a turn. Use a bounded queue and yield through appropriate task scheduling when needed.

Assuming parallel work

queueMicrotask does not create a background thread.

Its callback still runs on this page execution thread. Use a web worker when genuinely separate computation is appropriate.

What's going on

A microtask is a queued job that runs after the current JavaScript work finishes, before the next ordinary task is selected. Macrotask is a common informal name for an ordinary task, such as a timer callback. This example shows the order when a microtask adds another microtask.

The script first queues a timer, then a queueMicrotask callback, then a promise reaction. It prints its two synchronous lines before any of those callbacks. At the microtask checkpoint, C runs first because it entered the microtask queue first. It adds D2 to the back of that queue, behind the promise reaction D1 that is already waiting.

The browser continues processing the microtask queue until it is empty, so D1 and D2 both appear before the timer E. This is useful when reasoning about callbacks, but repeatedly adding microtasks without an end can delay timers and screen updates. A microtask is not a separate worker thread and is not a way to make heavy work run in parallel.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

What is the order after C?

Pick an answer to check it.

Question 2

What can an endless microtask chain delay?

Pick an answer to check it.

Questions people ask

Why does D1 appear before D2?

D1 was already queued when C ran. C appends D2 to the end of the same microtask queue.

Are promise then handlers microtasks?

Yes, promise reactions use the microtask queue in this browser example.

Can I use microtasks to run a large loop without freezing the page?

Not by itself. Heavy work inside a microtask still blocks other work, and an endless chain can delay the page further.