Skip to content

Instantly share code, notes, and snippets.

@srebalaji
Last active April 6, 2020 09:49
Show Gist options
  • Save srebalaji/eb750194de444feba9afbe1dd47fc83e to your computer and use it in GitHub Desktop.
Save srebalaji/eb750194de444feba9afbe1dd47fc83e to your computer and use it in GitHub Desktop.
NodeJS Worker thread example
// index.js
const {Worker, isMainThread, parentPort, workerData} = require('worker_threads')
const worker = new Worker("./worker.js", {workerData: {a: 5, b: 10}})
worker.once('message', (result) => {
console.log('The sum is', result)
})
// worker.js
const { parentPort, workerData } = require('worker_threads')
parentPort.postMessage(workerData.a + workerData.b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment