Skip to content

Instantly share code, notes, and snippets.

@deepal
Created December 8, 2019 23:55
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 deepal/3eef8cb3c6b351780ffef8c48f954f97 to your computer and use it in GitHub Desktop.
Save deepal/3eef8cb3c6b351780ffef8c48f954f97 to your computer and use it in GitHub Desktop.
worker-threads-simple
const {Worker, isMainThread, parentPort, workerData} = require('worker_threads');
if (isMainThread) {
const worker = new Worker(__filename, {workerData: {num: 5}});
worker.once('message', (result) => {
console.log('square of 5 is :', result);
})
} else {
parentPort.postMessage(workerData.num * workerData.num)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment