Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

queueMicrotask — scheduling a microtask

queueMicrotask(callback) schedules a short function in the microtask queue. It runs after the current synchronous code finishes, without scheduling a separate timer task or yielding time to the browser for rendering.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

queueMicrotask(callback)

queueMicrotask(callback) is a Web API defined by the HTML Living Standard, not a function assigned to a particular ECMAScript edition. It is exposed in window and worker contexts. It accepts a function called with no arguments and returns undefined, not a Promise. Check typeof queueMicrotask === 'function' in the target environment; the Node version used by a repository does not establish browser support.

The callback does not interrupt current synchronous code. It runs at a microtask checkpoint when the execution stack is empty. The queue drains in insertion order, including microtasks added while it is being processed. In the example, microtask:3 is added after the already waiting microtask:2.

Unlike setTimeout(callback, 0), this does not schedule a separate timer task. It suits ordering short updates after synchronous code. It does not move work to another thread: long callbacks or continually adding microtasks can block rendering and the processing of subsequent tasks.

An exception thrown by the callback is reported as an error rather than turned into a promise rejection as in a Promise.then callback. An outer try...catch around the call to queueMicrotask itself cannot catch that later exception; handle it inside the callback if needed.

Examples

(() => {
  const order = [];
  order.push('sync:start');

  queueMicrotask(() => {
    order.push('microtask:1');
    queueMicrotask(() => {
      order.push('microtask:3');
      console.log(order.join(' -> '));
    });
  });

  queueMicrotask(() => order.push('microtask:2'));
  order.push('sync:end');
})();

Result

sync:start -> sync:end -> microtask:1 -> microtask:2 -> microtask:3

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources