Task Queue
The task queue (macrotask queue) holds work the event loop runs one at a time: timers, I/O callbacks, UI events. After each task, the engine drains all microtasks before taking the next task.
Detailed Explanation
"Task queue" is the HTML spec name for what people call the macrotask queue. It is not one array in every host. Browsers have several task sources (timer, DOM events, networking, user interaction) so they can prioritise. Node has phases: timers, pending callbacks, poll, check (setImmediate), close. The programmer's model is still: a task runs to completion, then microtasks, then another task.
What counts as a task:
setTimeoutandsetIntervalcallbacks- I/O completion callbacks (Node
fs.readFile, socket data) - UI events (
click,keydown) in browsers setImmediatein Node (check phase)MessageChannelandpostMessageto self (a common "next task" trick)- script evaluation of a classic
<script>
What does not count: Promise.then, queueMicrotask, MutationObserver, await continuations. Those are microtasks. requestAnimationFrame is a rendering step, not a task in this list.
setTimeout(fn, 0) means "not sooner than 0 ms, as a task". Nested timeouts in browsers are clamped (historically 4 ms). It is not "run right after this line". Anything you await or .then will run before that timeout, because microtasks drain first.
A task that never returns (an infinite while) starves the queue. Timers, clicks, and I/O callbacks wait. The page or process looks frozen. Yield with await and a timer, or move work to a worker.
What it costs you: assuming order that the spec does not give you. setTimeout(0) vs setImmediate at the top level of a Node script is not stable. Inside an I/O callback, setImmediate runs before the next timers phase. Two hosts can order the same code differently if you mix APIs.
Reach for a task when you want to let the browser paint or Node process I/O before continuing. Reach for a microtask when you want to run after the current stack but before paint and before timers. Reach for a worker when the work is heavy enough that even one task would jank.
Code Examples
console.log("A");
setTimeout(() => console.log("D task"), 0);
Promise.resolve().then(() => console.log("C microtask"));
console.log("B");
// => A B C microtask D taskconst { once } = require("node:events");
// Node: inside I/O, setImmediate is queued in check, timeout in next timers phase.
const fs = require("node:fs");
fs.readFile(__filename, () => {
setTimeout(() => console.log("timeout"), 0);
setImmediate(() => console.log("immediate"));
});
// => immediate
// => timeoutfunction yieldToTasks() {
return new Promise((resolve) => setTimeout(resolve, 0));
}
async function chunked(items, work) {
for (let i = 0; i < items.length; i += 1) {
work(items[i]);
if (i % 500 === 0) await yieldToTasks();
}
}Common Gotchas
`setTimeout(0)` is a later task, not a microtask
promises scheduled now still run first.
Hosts have multiple queues and phases
do not write tests that depend on `setTimeout` vs `setImmediate` at the top level.
A never-ending task starves everything
there is no watchdog inside your loop. Yield or use a worker.
Nested browser timeouts are clamped
"0 ms" can become several milliseconds under load.