queueMicrotask()
Schedules a callback to run in the microtask queue.
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
| Environment | Since version |
|---|---|
| chrome | 71 |
| firefox | 69 |
| safari | 12.1 |
| edge | 79 |
| node | 11.0 |