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
- The Call Stack
- Macrotasks and the Task Queue
- Microtasks and Promises
- queueMicrotask and Ordering