1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 15:24:46 -05:00
denoland-deno/tests/testdata/workers/worker_ids_are_sequential.ts

34 lines
861 B
TypeScript

import {
isMainThread,
parentPort,
threadId,
Worker,
} from "node:worker_threads";
console.log("threadId", threadId);
if (isMainThread) {
const worker = new Worker(new URL(import.meta.url));
worker.on("message", (msg) => console.log("from worker:", msg));
worker.on("error", () => {
throw new Error("error");
});
worker.on("exit", (code) => {
if (code !== 0) {
reject(new Error(`Worker stopped with exit code ${code}`));
}
});
} else if (threadId == 1) {
const worker = new Worker(new URL(import.meta.url));
worker.on("message", (msg) => console.log("from worker:", msg));
worker.on("error", () => {
throw new Error("error");
});
worker.on("exit", (code) => {
if (code !== 0) {
reject(new Error(`Worker stopped with exit code ${code}`));
}
});
} else {
parentPort.postMessage("hello!");
}