1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-27 16:10:57 -05:00
denoland-deno/tests/specs/node/worker_threads/message_port.mjs

41 lines
1 KiB
JavaScript

import workerThreads from "node:worker_threads";
const { port1: mainPort, port2: workerPort } = new workerThreads
.MessageChannel();
// Note: not using Promise.withResolver() because it's not available in Node.js
const deferred = createDeferred();
const worker = new workerThreads.Worker(
import.meta.resolve("./message_port_1.cjs"),
{
workerData: workerPort,
transferList: [workerPort],
},
);
worker.on("message", (data) => {
console.log("worker:", data);
mainPort.on("message", (msg) => {
console.log("mainPort:", msg);
deferred.resolve();
});
mainPort.on("close", (_msg) => {
console.log("mainPort closed");
});
});
worker.postMessage("Hello from parent");
await deferred.promise;
await worker.terminate();
mainPort.close();
function createDeferred() {
let resolveCallback;
let rejectCallback;
const promise = new Promise((resolve, reject) => {
resolveCallback = resolve;
rejectCallback = reject;
});
return { promise, resolve: resolveCallback, reject: rejectCallback };
}