Chapter 10: Working with Workers samples
This commit is contained in:
@@ -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);
|
||||
}
|
||||
Reference in New Issue
Block a user