1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-23 15:16:54 -05:00
denoland-deno/tests/testdata/workers/worker_ids_are_sequential.ts

35 lines
861 B
TypeScript
Raw Normal View History

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!");
}