Created
June 10, 2023 02:19
-
-
Save barcellos-pedro/3fdfa68bcf30523b899062bd4e2b79f4 to your computer and use it in GitHub Desktop.
Workers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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