Skip to content

Instantly share code, notes, and snippets.

@barcellos-pedro
Created June 10, 2023 02:19
Show Gist options
  • Save barcellos-pedro/3fdfa68bcf30523b899062bd4e2b79f4 to your computer and use it in GitHub Desktop.
Save barcellos-pedro/3fdfa68bcf30523b899062bd4e2b79f4 to your computer and use it in GitHub Desktop.
Workers
const { Worker, isMainThread } = require("node:worker_threads");
const worker = new Worker("./report.js", {
workerData: { message: "hello from app.js" },
});
console.log("Running worker: ", worker.threadId);
console.log("is main thread (app.js) => ", isMainThread);
worker.on("error", (err) =>
console.error("error from worker => ", err.message)
);
worker.on("message", (data) => {
console.log("message from worker => ", data);
});
const { parentPort, workerData, isMainThread } = require("node:worker_threads");
console.log("is main thread (report.js) => ", isMainThread);
console.log("message from main => ", workerData);
// throw new Error("deu ruim")
parentPort.postMessage({ id: 1, name: "pedro" });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment