Skip to content
Tutorials Center
My learning

Topics

JavaScript Reference ยท Global Functions and Values

The queueMicrotask() function

Reference for queueMicrotask(): scheduling a callback in the microtask queue, how it orders against setTimeout and promises, and error behavior.

queueMicrotask() schedules a function to run as a microtask: after the current script or callback finishes, but before the event loop moves on to timers, rendering or the next event. It is the same queue that promise reactions (then, await) use.

Reach for it when you must defer work just a little, for example to batch several synchronous updates into one, without waiting for a full timer tick.

JavaScript
queueMicrotask(() => console.log("later, but soon"));

Try it

JS Ordering

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Batching updates

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Syntax

JavaScript
queueMicrotask(callback)
Name Description
callback The function to run. It receives no arguments and its return value is ignored.

Return value: undefined. There is no handle, so a queued microtask cannot be cancelled.

Exceptions: a TypeError if callback is not a function.

Usage notes

  • Order: microtasks run in first-in, first-out order after the current synchronous code, and always before the next macrotask such as a setTimeout callback.
  • Microtasks can queue more microtasks, and all of them run before the browser continues. A microtask that keeps re-queuing itself starves rendering and input, so avoid endless chains.
  • Errors are reported, not caught. An exception inside the callback appears as an uncaught error and cannot be handled by a try...catch around queueMicrotask().
  • Compared with setTimeout(fn, 0): the timeout waits for a later turn of the event loop (and is clamped by timers), while a microtask runs sooner and cannot be delayed by other tasks.
  • Compared with Promise.resolve().then(fn): the timing is the same, but queueMicrotask() states the intent and creates no promise, so thrown errors do not become rejections.
  • Not parallel. It does not run on another thread; heavy work still blocks the page. For that, use a Web Worker.
  • Ordering with await: code after await continues as a microtask too, so the two interleave in queue order.

Browser support

queueMicrotask() is available in all current browsers and in Node.js, Deno and Bun.