Web APIs

Worker()

Try the free JavaScript compiler

Creates a dedicated worker for a script URL; module type is optional. Creation may fail under origin or policy restrictions.

Formal Syntax & Structure

javascript syntaxjavascript
new Worker(scriptURL, [options])

Parameters & Attributes

NameTypeDescription
scriptURLstring | URLWorker script location.
optionsobjectOptional worker type, credentials and name.

Return Value

Worker

Creates a dedicated worker for a script URL; module type is optional. Creation may fail under origin or policy restrictions.

Working Example

Example Snippetjavascript
(async () => {const url=URL.createObjectURL(new Blob(['self.onmessage=e=>self.postMessage(e.data.reduce((a,b)=>a+b,0));'],{type:"text/javascript"}));let w;try{w=new Worker(url);const result=new Promise((resolve,reject)=>{w.onmessage=e=>resolve(e.data);w.onerror=e=>reject(new Error(e.message));});w.postMessage([2,3,4]);console.log(JSON.stringify(await result));}finally{w?.terminate();URL.revokeObjectURL(url);}})()
JavaScript Reference