Workers

worker.terminate()

Requests worker termination and resolves after exit. Prefer cooperative cancellation for work requiring cleanup; do not assume finally blocks will finish after termination. Tested on Node.js 22.16.0. Example output (one line per entry): ["true"].

Formal Syntax & Structure

nodejs syntaxnodejs
worker.terminate()

Return Value

Promise<number>

Exit code when terminated.

Working Example

Example Snippetnodejs
import { Worker } from 'node:worker_threads';import { once } from 'node:events';const w = new Worker("setInterval(()=>{},1000);require('node:worker_threads').parentPort.postMessage('ready')", { eval: true });try {    await once(w, 'message');    console.log(Number.isInteger(await w.terminate()));}finally {    await w.terminate();}
Node.js Reference