diff --git a/ext/node/polyfills/worker_threads.ts b/ext/node/polyfills/worker_threads.ts index 71999dd624..36314675ac 100644 --- a/ext/node/polyfills/worker_threads.ts +++ b/ext/node/polyfills/worker_threads.ts @@ -32,6 +32,7 @@ import process from "node:process"; const { JSONParse, JSONStringify, ObjectPrototypeIsPrototypeOf } = primordials; const { Error, + PromiseResolve, Symbol, SymbolFor, SymbolIterator, @@ -280,7 +281,8 @@ class NodeWorker extends EventEmitter { this.#status = "TERMINATED"; op_host_terminate_worker(this.#id); } - this.emit("exit", 1); + this.emit("exit", 0); + return PromiseResolve(0); } ref() { diff --git a/tests/unit_node/worker_threads_test.ts b/tests/unit_node/worker_threads_test.ts index f46d982fe6..e16bc89666 100644 --- a/tests/unit_node/worker_threads_test.ts +++ b/tests/unit_node/worker_threads_test.ts @@ -516,6 +516,32 @@ Deno.test({ }, }); +Deno.test({ + name: "[node/worker_threads] Returns terminate promise with exit code", + async fn() { + const deferred = Promise.withResolvers(); + const worker = new workerThreads.Worker( + ` + import { parentPort } from "node:worker_threads"; + parentPort.postMessage("ok"); + `, + { + eval: true, + }, + ); + + worker.on("message", (data) => { + assertEquals(data, "ok"); + deferred.resolve(); + }); + + await deferred.promise; + const promise = worker.terminate(); + assertEquals(typeof promise.then, "function"); + assertEquals(await promise, 0); + }, +}); + Deno.test({ name: "[node/worker_threads] MessagePort.on all message listeners are invoked",