Exciting news!Announcing our $4.6M Series Seed. Learn more
Socket
LoveBlogFAQ
Install
Log in

promisify-worker

Package Overview
Dependencies
1
Maintainers
1
Versions
2
Issues
File Explorer

Advanced tools

promisify-worker

A promisification interface for web workers

    0.1.1latest

Version published
Maintainers
1
Yearly downloads
5,005
increased by4.27%

Weekly downloads

Readme

Source

promisify-worker

npm version

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/

Example

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 }); } });

Use

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.

Important notes

This library uses and does not transpile Proxies, these are available in all modern browsers.

FAQs

What is promisify-worker?

A promisification interface for web workers

Is promisify-worker popular?

The npm package promisify-worker receives a total of 191 weekly downloads. As such, promisify-worker popularity was classified as not popular.

Is promisify-worker well maintained?

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.

Last updated on 17 Feb 2018
Socket

Product

Subscribe to our newsletter

Get open source security insights delivered straight into your inbox. Be the first to learn about new features and product updates.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc