0Pricing
JavaScript Academy · Lesson

Macrotasks and the Task Queue

Understand how timers and I/O are scheduled.

What Is a Macrotask?

A macrotask (also called a task) is a unit of work scheduled to run later, after the current synchronous code finishes. setTimeout callbacks are the classic example.

Macrotasks wait in the task queue until the call stack is empty.

setTimeout Defers Work

setTimeout registers a callback to run later. Even with a delay of 0, it runs after all current synchronous code.

console.log('first');
setTimeout(() => console.log('third (timeout)'), 0);
console.log('second');

All lessons in this course

  1. The Call Stack
  2. Macrotasks and the Task Queue
  3. Microtasks and Promises
  4. queueMicrotask and Ordering
← Back to JavaScript Academy