Created
March 17, 2019 00:37
-
-
Save JeffML/730dcf5fa2bed3c41688ef3287476f3c to your computer and use it in GitHub Desktop.
This file contains hidden or 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("worker_threads"); | |
function runService(workerData) { | |
const worker = new Worker("./service.js", { workerData }); | |
worker.postMessage("once"); | |
worker.on("message", incoming => console.log({ incoming })); | |
worker.on("error", code => new Error(`Worker error with exit code ${code}`)); | |
worker.on("exit", code => | |
console.log(`Worker stopped with exit code ${code}`) | |
); | |
worker.postMessage("twice"); | |
worker.postMessage("three times"); | |
worker.postMessage("exit"); | |
setTimeout(() => worker.postMessage("you won't see me"), 100); | |
} | |
async function run() { | |
const result = runService("let's begin"); | |
console.log({ isMainThread }); | |
} | |
run().catch(err => console.error(err)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment