Worker lifecycle, failure and cooperative stopping
Quick answer
A worker has an independent lifetime. Termination stops its execution; cooperative cancellation instead asks the worker to stop at a defined boundary.
How it works
worker.terminate() requests immediate termination and does not wait for cleanup. Inside a dedicated worker, self.close() closes its event loop to further tasks after the current task. An error listener handles reported runtime errors; validate incoming messages and send explicit result/error records for expected failures. A long synchronous job cannot process a cancellation message until it returns control; break work into bounded chunks or use a designed atomic cancellation flag.
Local examples
Run in a fresh browser document with local synthetic data. Actual ByteMind editor support is unverified.
(async () => {const url=URL.createObjectURL(new Blob(['self.onmessage=e=>self.postMessage(e.data);'],{type:"text/javascript"}));let w;try{w=new Worker(url);console.log(String(w.terminate()));}finally{w?.terminate();URL.revokeObjectURL(url);}})()Expected console output:
undefinedTermination does not return a result or completion promise.
Run in a fresh browser document with local synthetic data. Actual ByteMind editor support is unverified.
(async () => {const url=URL.createObjectURL(new Blob(['self.onmessage=()=>{throw new Error("demo");};'],{type:"text/javascript"}));let w;try{w=new Worker(url);const failed=new Promise(ok=>w.addEventListener("error",e=>{e.preventDefault();ok(e.message.includes("demo"));},{once:true}));w.postMessage(null);console.log(await failed);}finally{w?.terminate();URL.revokeObjectURL(url);}})()Expected console output:
trueThe worker error reaches the registered listener.
Common mistakes
Do not rely on a terminated worker to send its final acknowledgement. Reject or settle outstanding requests on the page and release URLs, listeners and resources.
Practice
Implement a worker returning explicit error records for invalid integer input. Describe what the page should do to pending requests if it terminates the worker.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Worker lifecycle, failure and cooperative stopping — Knowledge Check
Apply the concept and reason about local example output.
Does terminate() await the worker’s cleanup?
Why may a cancellation message arrive too late for a synchronous job?
How should expected invalid job input be represented?
What is the expected console output of Worker lifecycle, failure and cooperative stopping example 1?
(async () => {const url=URL.createObjectURL(new Blob(['self.onmessage=e=>self.postMessage(e.data);'],{type:"text/javascript"}));let w;try{w=new Worker(url);console.log(String(w.terminate()));}finally{w?.terminate();URL.revokeObjectURL(url);}})()What is the expected console output of Worker lifecycle, failure and cooperative stopping example 2?
(async () => {const url=URL.createObjectURL(new Blob(['self.onmessage=()=>{throw new Error("demo");};'],{type:"text/javascript"}));let w;try{w=new Worker(url);const failed=new Promise(ok=>w.addEventListener("error",e=>{e.preventDefault();ok(e.message.includes("demo"));},{once:true}));w.postMessage(null);console.log(await failed);}finally{w?.terminate();URL.revokeObjectURL(url);}})()