CKTIS / JavaScript
Worker.terminate(): forcibly stopping a worker
worker.terminate() stops a dedicated worker from code holding its Worker object. The method takes no arguments and returns undefined; it does not guarantee completion of the current task or execution of cleanup code.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
worker.terminate()worker.terminate() is a Worker instance method defined by the HTML Standard, not ECMAScript. It stops a dedicated worker. Code holding its Worker object calls it.
Termination discards the worker's queued tasks and aborts its running script. Do not rely on a finally block running or a final message being sent.
The method takes no arguments, returns undefined, and provides no completion promise. self.close() acts inside the worker and lets the current task finish. When cleanup matters, design a message protocol with an acknowledgment before forced termination.
The example receives 42, calls terminate(), and releases the Blob object URL. Run it in a browser allowing blob: workers; the expected output is one line containing 42. The output alone does not prove that the worker terminated.
Examples
const url = URL.createObjectURL(new Blob([
'self.onmessage = () => self.postMessage(42);'
], { type: 'text/javascript' }));
const worker = new Worker(url);
worker.onmessage = ({ data }) => {
worker.terminate();
URL.revokeObjectURL(url);
console.log(data);
};
worker.postMessage('start');Result
42
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.