queueMicrotask
Queues a microtask to be executed before control returns to the browser's event loop
Syntax
queueMicrotask(callback)Parameters
| Parameter | Type | Description |
|---|---|---|
| callback | () => void | The function to be executed as a microtask |
Return Value
undefined
Examples
queueMicrotask(() => {
console.log('microtask');
});
console.log('sync');
// Output: 'sync', 'microtask'console.log('1');
setTimeout(() => console.log('4'), 0);
queueMicrotask(() => console.log('2'));
console.log('3');
// Output: 1, 3, 2, 4function flush(tasks: (() => void)[]) {
while (tasks.length) {
const task = tasks.shift()!;
queueMicrotask(task);
}
}Understanding queueMicrotask
The queueMicrotask method in JavaScript queues a microtask to be executed before control returns to the browser's event loop. It belongs to the window object and is one of the most widely used methods for working with window values in modern JavaScript and TypeScript applications.
The method signature is queueMicrotask(callback). It accepts 1 parameter: callback. When called, it returns undefined. Understanding when and how to use queueMicrotask() helps you write more expressive, readable code.
Common use cases for queueMicrotask include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like window-settimeout, promise-then, promise-resolve, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for queueMicrotask is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
More Global / Window Methods
Other methods in the Global / Window object
Related Tools
More Global / Window Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.