Window: queueMicrotask() method - Web APIs | MDN

Syntax

js

queueMicrotask(callback)

Parameters

callback

A function to be executed when the browser engine determines it is safe to call your code. Enqueued microtasks are executed after all pending tasks have completed but before yielding control to the browser's event loop.

Return value

None (undefined).

Examples

js

queueMicrotask(() => {
  // function contents here
});

Taken from the queueMicrotask spec:

js

MyElement.prototype.loadData = function (url) {
  if (this._cache[url]) {
    queueMicrotask(() => {
      this._setData(this._cache[url]);
      this.dispatchEvent(new Event("load"));
    });
  } else {
    fetch(url)
      .then((res) => res.arrayBuffer())
      .then((data) => {
        this._cache[url] = data;
        this._setData(data);
        this.dispatchEvent(new Event("load"));
      });
  }
};

Specifications

Specification
HTML
# microtask-queuing

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on by MDN contributors.