Chapter 10: Working with Workers samples

This commit is contained in:
Beth Griggs
2020-08-31 01:12:46 +01:00
parent 575ab40a00
commit 52d632f0d2
3 changed files with 66 additions and 0 deletions
+20
View File
@@ -0,0 +1,20 @@
const {
Worker,
isMainThread,
parentPort,
workerData,
} = require("worker_threads");
if (isMainThread) {
// Main thread code
const worker = new Worker(__filename, {
workerData: "Beth",
});
worker.on("message", (msg) => {
console.log(msg);
});
} else {
// Worker code
const greeting = `Hello ${workerData}!`;
parentPort.postMessage(greeting);
}