WindowOrWorkerGlobalScope.queueMicrotask

The queueMicrotask() method of the WindowOrWorkerGlobalScope mixin queues a microtask.

Syntax

scope.queueMicrotask(function);

Parameters

function
A function to be executed.

Polyfill

if (typeof window.queueMicrotask !== "function") {
  window.queueMicrotask = function (callback) {
    Promise.resolve()
      .then(callback)
      .catch(e => setTimeout(() => { throw e; }));
  };
}

Notes

Infinite loops

A microtask posted with queueMicrotask may itself queue another microtask, thus possibly creating an infinite loop. This will cause the page to become unresponsive.

Specifications

Specification Status Comment
HTML Living Standard
The definition of 'self.queueMicrotask()' in that specification.
Living Standard Initial definition

Browser compatibility

No compatibility data found. Please contribute data for "api.WindowOrWorkerGlobalScope.queueMicrotask" (depth: 1) to the MDN compatibility data repository.

Since Chrome 71: https://www.chromestatus.com/feature/5111086432911360
Since Node.js v11.0.0: https://nodejs.org/api/globals.html#globals_queuemicrotask_callback
Since Safari 12.1
Since Firefox 69

See also

Document Tags and Contributors

Contributors to this page: DomenicDenicola, zbjornson
Last updated by: DomenicDenicola,