The JavaScript event queue, also called the task queue or callback queue, is where finished asynchronous work waits to run. When a timer fires, a click happens, or a network response arrives, its callback is placed in the queue. The event loop moves a callback onto the call stack only when the stack is empty, so JavaScript can stay single-threaded and still react to events. A second, higher-priority queue holds microtasks such as promise callbacks.
Four pieces work together:
Because a callback runs only when the stack is empty, a long-running synchronous function delays everything else, including rendering and clicks.
There is more than one queue. The HTML standard defines tasks (often called macrotasks) and microtasks.
| Queue | Examples | When it runs |
|---|---|---|
| Task (macrotask) queue | setTimeout, setInterval, DOM events, MessageChannel, network callbacks | One task per loop turn |
| Microtask queue | Promise .then, async and await continuations, queueMicrotask, MutationObserver | After the current task, before the next task and before rendering |
After each task finishes, the engine drains the whole microtask queue, including microtasks added while draining. Only then can the browser render or run the next task. This is why a promise callback always runs before a setTimeout callback scheduled with a delay of 0.
Here is the order of events:
console.log lines and the scheduling calls.setTimeout(fn, 0) does not mean "now". It means "as soon as possible after the current work and the microtasks", and browsers may clamp nested timers to at least a few milliseconds.
A heavy synchronous loop keeps the stack busy, so queued callbacks cannot run and the page freezes.
The timer was ready after a few milliseconds, but it ran only after the two-second block. To keep an interface responsive:
setTimeout or scheduler.yield() where supported.fetch over blocking ones.Node.js uses libuv and has additional phases such as timers, poll, and check (setImmediate). process.nextTick callbacks run before promise microtasks. The core idea is the same, but the exact order of setTimeout(fn, 0) and setImmediate can vary in the main module.
No. The call stack holds functions that are running now. The event queue holds callbacks that are waiting for the stack to be empty.
Not on a single thread. The runtime performs I/O and timers in parallel, but your JavaScript callbacks run one at a time. Workers give true parallelism with separate threads and message passing.
Promise callbacks are microtasks, which are drained right after the current task. A timer callback is a task and waits for the next loop turn.
No. The queue is the data structure. The loop is the process that takes items from it.
The event queue holds callbacks that are ready to run, and the event loop moves them to the call stack when it is empty. Microtasks (promises, await) run before the next task, so they run before timers. Keep synchronous work short, and use workers or chunking for heavy computation.