Course contentsShow
← JavaScript
Lesson 1191 of 2,72327. The Event LoopFree lesson

Two Queues: Tasks and Microtasks

Learn that JavaScript has separate task and microtask queues with different priorities.

Two Queues: Tasks and Microtasks

What you'll learn: JavaScript doesn't have just one waiting line for asynchronous work—it has two separate queues with different priorities.

The Two-Queue System

While the call stack handles synchronous code, JavaScript manages asynchronous operations through two distinct queues:

  1. Task Queue (also called Macrotask Queue): Holds regular asynchronous callbacks
  2. Microtask Queue: Holds higher-priority asynchronous callbacks

Think of it like airport boarding: first-class passengers (microtasks) board before economy passengers (tasks), even if economy passengers arrived at the gate first.

Why Two Queues?

JavaScript separates these queues to give certain operations priority. When the call stack empties:

  1. All microtasks run first, completely emptying the microtask queue
  2. Then one task runs from the task queue
  3. The cycle repeats

This means microtasks always "cut in line" ahead of tasks.

What Goes Where?

Microtask Queue:

  • Promise callbacks (.then(), .catch(), .finally())
  • queueMicrotask() callbacks
  • Mutation observer callbacks

Task Queue:

  • setTimeout() and setInterval() callbacks
  • DOM events (clicks, scrolls, etc.)
  • Network request completions
  • setImmediate() (Node.js)

The Priority Impact

This priority system ensures promises resolve as quickly as possible without waiting for other queued work. If you schedule both a promise and a timeout, the promise callback will run first, regardless of timing.

Key Takeaway: JavaScript uses two separate queues—microtasks (high priority) and tasks (regular priority)—with all microtasks running before the next task gets its turn.