Worker - Web APIs | MDN

archived 18 Jun 2023 00:09:47 UTC

Worker

The Worker interface of the Web Workers API represents a background task that can be created via script, which can send messages back to its creator.
Creating a worker is done by calling the Worker("path/to/worker/script") constructor.
Workers may themselves spawn new workers, as long as those workers are hosted at the same origin as the parent page.
Not all interfaces and functions are available to scripts inside a Worker. Workers may use XMLHttpRequest for network communication, but its responseXML and channel attributes are always null. (fetch is also available, with no such restrictions.)
EventTarget Worker

#Constructors

Worker()
Creates a dedicated web worker that executes the script at the specified URL. This also works for Blob URLs.

#Instance properties

Inherits properties from its parent, EventTarget.

#Instance methods

Inherits methods from its parent, EventTarget.
Worker.postMessage()
Sends a message — consisting of any JavaScript object — to the worker's inner scope.
Worker.terminate()
Immediately terminates the worker. This does not let worker finish its operations; it is halted at once. ServiceWorker instances do not support this method.

#Events

error
Fires when an error occurs in the worker.
message
Fires when the worker's parent receives a message from that worker.
messageerror
Fires when a Worker object receives a message that can't be deserialized.
rejectionhandled
Fires every time a Promise rejects, regardless of whether or not there is a handler to catch the rejection.
unhandledrejection
Fires when a Promise rejects with no handler to catch the rejection.

#Example

The following code snippet creates a Worker object using the Worker() constructor, then uses the worker object:
const myWorker = new Worker("/worker.js");
const first = document.querySelector("input#number1");
const second = document.querySelector("input#number2");

first.onchange = () => {
  myWorker.postMessage([first.value, second.value]);
  console.log("Message posted to worker");
};

#Specifications

Specification
HTML Standard
# dedicated-workers-and-the-worker-interface

#Browser compatibility

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
Deno
Node.js
Worker
Worker() constructor
Support for ECMAScript modules
Strict MIME type checks for worker scripts
options.name parameter
options.type parameter
error event
message event
messageerror event
postMessage
terminate

Legend

Tip: you can click/tap on a cell for more information.
Full supportFull support
Partial supportPartial support
No supportNo support
See implementation notes.
User must explicitly enable this feature.
Has more compatibility info.
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Support varies for different types of workers. See each worker type's page for specifics.

#Cross-origin worker error behavior

In early versions of the spec, loading a cross-origin worker script threw a SecurityError. Nowadays, an error event is thrown instead.

#See also

0%
10%
20%
30%
40%
50%
60%
70%
80%
90%
100%