queueMicrotask()

Schedules a callback to run in the microtask queue.

Since ES2015 (ES6) Spec ↗

Syntax

queueMicrotask(callback)

Returns

undefined — Returns `undefined`.

Examples

console.log("start");
queueMicrotask(() => console.log("microtask"));
console.log("end");
Output
start
end
microtask
setTimeout(() => console.log("timeout"), 0);
queueMicrotask(() => console.log("micro"));
Output
micro
timeout

Notes

- Microtasks run after the current task and before the next macrotask (timers, I/O). - Equivalent timing to a resolved promise's `.then()` callback. - Avoid infinite microtask loops, which can starve the event loop.

Browser & runtime support

EnvironmentSince version
chrome 71
firefox 69
safari 12.1
edge 79
node 11.0

See also