Methods
Method: run(task[, options])
Schedules a task to be run on a Worker thread.
task: Any value. This will be passed to the function that is exported fromfilename.options:transferList: An optional lists of objects that is passed to [postMessage()] when postingtaskto the Worker, which are transferred rather than cloned.filename: Optionally overrides thefilenameoption passed to the constructor for this task. If nofilenamewas specified to the constructor, this is mandatory.name: Optionally overrides the exported worker function used for the task.abortSignal: AnAbortSignalinstance. If passed, this can be used to cancel a task. If the task is already running, the correspondingWorkerthread will be stopped. (More generally, anyEventEmitterorEventTargetthat emits'abort'events can be passed here.) Abortable tasks cannot share threads regardless of theconcurrentTasksPerWorkeroptions.
This returns a Promise for the return value of the (async) function call
made to the function exported from filename. If the (async) function throws
an error, the returned Promise will be rejected with that error.
If the task is aborted, the returned Promise is rejected with an error
as well.
Method: runTask(task[, transferList][, filename][, abortSignal])
Deprecated -- Use run(task, options) instead.
Schedules a task to be run on a Worker thread.
task: Any value. This will be passed to the function that is exported fromfilename.transferList: An optional lists of objects that is passed to [postMessage()] when postingtaskto the Worker, which are transferred rather than cloned.filename: Optionally overrides thefilenameoption passed to the constructor for this task. If nofilenamewas specified to the constructor, this is mandatory.signal: An [AbortSignal][] instance. If passed, this can be used to cancel a task. If the task is already running, the correspondingWorkerthread will be stopped. (More generally, anyEventEmitterorEventTargetthat emits'abort'events can be passed here.) Abortable tasks cannot share threads regardless of theconcurrentTasksPerWorkeroptions.
This returns a Promise for the return value of the (async) function call
made to the function exported from filename. If the (async) function throws
an error, the returned Promise will be rejected with that error.
If the task is aborted, the returned Promise is rejected with an error
as well.
Method: destroy()
Stops all Workers and rejects all Promises for pending tasks.
This returns a Promise that is fulfilled once all threads have stopped.
Method: close([options])
options:force: Abooleanvalue that indicates whether to abort all tasks that are enqueued but not started yet. The default isfalse.
It stops all Workers gracefully.
This returns a Promise that is fulfilled once all tasks that were started
have completed and all threads have stopped.
This method is similar to destroy(), but with the difference that close()
will wait for the worker tasks to finish, while destroy()
will abort them immediately.