@belsrc/worker-promise
v0.4.0
Published
Wrap WebWorker interactions in a Promise or callback
Downloads
64
Readme
Worker Promise
Simple little Promise wrapper for WebWorkers. Make your workers pinky swear.
Install
npm i -s @belsrc/worker-promise
Use
// Example using webpack and worker-loader
import workerPromise from '@belsrc/worker-promise';
import Worker from './process.worker';
const worker = new Worker();
workerPromise(worker, { count: 500000 }).then(res => console.log(res));
also supports callbacks using the optional third parameter.
workerPromise(webworker, value[, callback])
with the callback being
function(error, value)
I don't advise using with async..await
as it still blocks.
// async-await
const res = await workerPromise(worker, { count: 500000 });
console.log(res);
// calling worker
// processing: 2136.183837890625ms
// {error: null, data: Array(41538)}
// back on the main thread
// promise-then
workerPromise(worker, { count: 500000 }).then(res => console.log(res));
// calling worker
// back on the main thread
// processing: 2149.153076171875ms
// {error: null, data: Array(41538)}
// callback
const logRes = (err, res) => console.log(res);
workerPromise(worker, { count: 500000 }, logRes);
// calling worker
// back on the main thread
// processing: 2131.60205078125ms
// {error: null, data: Array(41538)}