blob: 19ef3803f305a4b637e1f130cc7a0bf025771223 [file]
// This example worker runs asynchronous tasks. In practice, this could be
// interacting with a database or a web service. The asynchronous function
// returns a promise which resolves with the task's result.
var workerpool = require('../..');
// an async function returning a promise
function asyncAdd (a, b) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
resolve(a + b);
}, 1000);
});
}
// an async function returning a promise
function asyncMultiply (a, b) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
resolve(a * b);
}, 1000);
});
}
// create a worker and register public functions
workerpool.worker({
asyncAdd: asyncAdd,
asyncMultiply: asyncMultiply
});