Skip to content

Instantly share code, notes, and snippets.

@questsin
Created February 20, 2019 15:11
Show Gist options
  • Save questsin/152f70b951b511d0f85186765be3e706 to your computer and use it in GitHub Desktop.
Save questsin/152f70b951b511d0f85186765be3e706 to your computer and use it in GitHub Desktop.
const spawn = require('threads').spawn;
const thread = spawn(function(input, done) {
// Everything we do here will be run in parallel in another execution context.
// Remember that this function will be executed in the thread's context,
// so you cannot reference any value of the surrounding code.
done({ string : input.string, integer : parseInt(input.string) });
});
thread
.send({ string : '123' })
// The handlers come here: (none of them is mandatory)
.on('message', function(response) {
console.log('123 * 2 = ', response.integer * 2);
thread.kill();
})
.on('error', function(error) {
console.error('Worker errored:', error);
})
.on('exit', function() {
console.log('Worker has been terminated.');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment