CKTIS / JavaScript
Event loop: tasks and microtasks
The event loop coordinates script execution and event handling in a browser. Distinguishing tasks, microtasks, and rendering opportunities helps you predict the order of asynchronous code.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
queueMicrotask(callback); setTimeout(callback, delay);The event loop is a browser-environment mechanism, not a JavaScript statement. Synchronous code generally runs to completion without interruption by another task. This does not mean the entire browser operates on one thread.
The loop selects a runnable task from one of its task queues. There is no single global FIFO queue for all sources. A separate microtask queue is drained at checkpoints, including after a task. Microtasks added while it is being drained also execute during that checkpoint.
queueMicrotask() schedules a microtask; setTimeout() schedules a later task. A delay of 0 means neither immediate execution nor a guaranteed execution time. In the example, synchronous code records sync and end, the microtask then appends microtask, and the timer task appends task and prints the result.
Long synchronous code and an endless chain of microtasks can delay interaction handling and rendering. Rendering need not happen after every task. This describes the HTML browser model; having Node 24 in a repository does not establish browser API support.
Examples
(() => {
const order = [];
order.push('sync');
setTimeout(() => {
order.push('task');
console.log(order.join(' -> '));
}, 0);
queueMicrotask(() => {
order.push('microtask');
});
order.push('end');
})();Result
sync -> end -> microtask -> task
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.