requestWorker
Runs one job in a module worker: starts it, posts the request, relays its progress, resolves
with its result and ends it, whatever the outcome (result, error, error event or abort).
The worker answers with WorkerReply messages; a result can carry transferables by
posting them as postMessage(reply, transferList).
Outside a browser (no Worker), a URL or a string rejects with a TypeError; a factory is
called as it is.
Available since v3.1.10
Import
Section titled “Import”Signature
Section titled “Signature”Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
worker | string | URL | function | The worker’s URL, or a function that creates it (needed for bundlers, which only see `new Worker(new URL(’./job.js’, import.meta.url), { type: ‘module’ })` written in place) |
request | Request | The message to post |
options | RequestWorkerOptions<Progress> | Transferables, progress and cancellation — see RequestWorkerOptions |
Returns
Section titled “Returns”Promise<Result> — The worker’s result
Examples
Section titled “Examples”Run a job with progress
Section titled “Run a job with progress”The worker replies with progress messages, then a result; it is ended afterwards.
Related Types
Section titled “Related Types”RequestWorkerOptions
Section titled “RequestWorkerOptions”Options of requestWorker.
WorkerReply
Section titled “WorkerReply”What a worker answers to requestWorker, through postMessage: any number of
{ type: 'progress', value }, then one { type: 'result', value } or { type: 'error', message }.
