Skip to content

Instantly share code, notes, and snippets.

@navanathjadhav
Created June 18, 2022 13:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save navanathjadhav/65143c47884956eab5b63d5b073aa69e to your computer and use it in GitHub Desktop.
Save navanathjadhav/65143c47884956eab5b63d5b073aa69e to your computer and use it in GitHub Desktop.
index.js, main thread file
/*
* index.js, main thread file
*/
const { Worker } = require("worker_threads");
function runService(workerData) {
return new Promise((resolve, reject) => {
const worker = new Worker("./worker.js", { workerData });
worker.on("message", resolve);
worker.on("error", reject);
worker.on("exit", (code) => {
if (code !== 0)
reject(
new Error(`Stopped the Worker Thread with the exit code: ${code}`)
);
});
});
}
async function run() {
const result = await runService("Ever Blogs");
console.log(result);
}
run().catch((err) => console.error(err));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment