diff options
Diffstat (limited to 'tests/specs/node/worker_threads/auto_exits.mjs')
-rw-r--r-- | tests/specs/node/worker_threads/auto_exits.mjs | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/tests/specs/node/worker_threads/auto_exits.mjs b/tests/specs/node/worker_threads/auto_exits.mjs new file mode 100644 index 000000000..e434f59f7 --- /dev/null +++ b/tests/specs/node/worker_threads/auto_exits.mjs @@ -0,0 +1,19 @@ +import { isMainThread, parentPort, Worker } from "node:worker_threads"; + +function onMessageOneshot() { + console.log("Got message from main thread!"); + parentPort.off("message", onMessageOneshot); +} + +if (isMainThread) { + // This re-loads the current file inside a Worker instance. + const w = new Worker(import.meta.filename); + + setTimeout(() => { + w.postMessage("Hello! I am from the main thread."); + }, 500); +} else { + console.log("Inside Worker!"); + console.log(isMainThread); // Prints 'false'. + parentPort.on("message", onMessageOneshot); +} |