Chapter XVI · Asynchronous JavaScript

Callback Queue

The callback queue (or macrotask queue) holds asynchronous task callbacks waiting to be pushed onto the call stack.

01 How It Works

Think of customers waiting in an orderly single-file line outside a service teller window.

When a Web API finishes its task, it places its callback into the callback queue. The event loop moves it to the stack when empty.

example.js
setTimeout(() => console.log("Task in queue"), 0);
  1. 1. Web API completes background task.

  2. 2. Callback is enqueued inside the callback queue.

  3. 3. Event loop dequeues it into the call stack when stack is clear.

02 Practical Example

Here is how you might see this concept applied in real-world code:

practical.js
setTimeout(() => console.log("Macrotask queue item"), 0);

Key Takeaway

The callback queue stores macrotasks waiting for execution by the JavaScript engine.

Related Concepts