queueMicrotask()
Queues a callback to run as a microtask after the current operation.
Syntax
queueMicrotask(callback) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
callback | function | Yes | The function to execute in the microtask queue. |
Returns
void — Returns nothing.
Examples
console.log('1');
queueMicrotask(() => console.log('3 (microtask)'));
console.log('2');
Output
1
2
3 (microtask)
function emitAsync(emitter, event, data) {
queueMicrotask(() => emitter.emit(event, data));
}
Notes
Runs at the same priority as resolved Promise callbacks, after the
current synchronous code and `process.nextTick`, but before timers
and I/O. Use it to defer work without the macrotask delay of
`setTimeout(fn, 0)`. Standard and cross-platform.