promisify-worker
Advanced tools
A promisification interface for web workers
Weekly downloads
Readme
A lightweight simple promisification interface for web workers. Allows you to dispatch messages with worker.postMessage
and receive the answer as a promise.
Install with npm i -S promisify-worker
.
Demo: https://andrewghc.github.io/promisify-worker/
This example uses Webpack's worker-loader.
Main thread
import promisifyWorker from 'promisify-worker';
import Worker from './your-web-worker-path';
async function doSomething(worker) {
const answer = await worker.postMessage({ command: 'hello' });
console.log(answer);
}
const worker = promisifyWorker(new Worker());
doSomething(worker);
Worker thread
self.addEventListener('message', ({ data: { command, messageId } }) => {
switch (command) {
case 'hello':
self.postMessage({ message: 'hi there', messageId });
break;
default:
self.postMessage({ message: 'this shouldn\'t happen!', messageId });
}
});
The exported function promisifyWorker
accepts two arguments:
promifyWorker(worker[, namespace = 'messageId'])
.
worker: A web worker.
namespace: A namespace used to identify events. Defaults to messageId
.
For this library to work, the worker thread must include the namespace when calling postMessage
. This also means that worker threads must respond with an object. Please see the example for this in practice.
This library uses and does not transpile Proxies, these are available in all modern browsers.
A promisification interface for web workers
The npm package promisify-worker receives a total of 191 weekly downloads. As such, promisify-worker popularity was classified as not popular.
We found that promisify-worker demonstrated a not healthy version release cadence and project activity. It has 1 open source maintainer collaborating on the project.