Snapshot of nolanlawson/promise-worker: 482★, JavaScript. Promise-based messaging for Web Workers and Service Workers
Snapshot summary built from the project's own GitHub metadata — there's no written TopGit review yet. The page will update automatically when a full review is published.
WHY NO REVIEW YET
TopGit writes full reviews for the most-starred, most-requested repositories. This page is a snapshot until then — see the READ ME tab for the original README in full.
A small and performant library for communicating with Web Workers or Service Workers, using Promises. Post a message to the worker, get a message back.
Goals:
Tiny footprint (~700 bytes min+gz)
Assumes you have a separate worker.js file (easier to debug, better browser support)
Live examples:
Web Workers
Service Workers
Usage
Install:
npm install promise-worker
Inside your main bundle:
// main.js
var PromiseWorker = require('promise-worker');
var worker = new Worker('worker.js');
var promiseWorker = new PromiseWorker(worker);
promiseWorker.postMessage('ping').then(function (response) {
// handle response
}).catch(function (error) {
// handle error
});
Note that stacktraces cannot be sent from the worker to the main thread, so you
will have to debug those errors yourself. This library does however, print
messages to console.error(), so you should see them there.
Multi-type messages
If you need to send messages of multiple types to the worker, just add
some type information to the message you send:
Communicating with a Service Worker is the same as with a Web Worker.
However, you have to wait for the Service Worker to install and start controlling the page. Here's an example:
navigator.serviceWorker.register('sw.js', {
scope: './'
}).then(function () {
if (navigator.serviceWorker.controller) {
// already active and controlling this page
return navigator.serviceWorker;
}
// wait for a new service worker to control this page
return new Promise(function (resolve) {
function onControllerChange() {
navigator.serviceWorker.removeEventListener('controllerchange', onControllerChange);
resolve(navigator.serviceWorker);
}
navigator.serviceWorker.addEventListener('controllerchange', onControllerChange);
});
}).then(function (worker) { // the worker is ready
var promiseWorker = new PromiseWorker(worker);
return promiseWorker.postMessage('hello worker!');
}).catch(console.log.bind(console));
Then inside your Service Worker:
var registerPromiseWorker = require('../register');
registerPromiseWorker(function (msg) {
return 'hello main thread!';
});
self.addEventListener('activate', function(event) {
event.waitUntil(self.clients.claim()); // activate right now
});
Browser support
Note that as of v2.0.0, promise-worker does not contain a built-in Promise
polyfill. Use something like es6-promise
if you need to support browsers that don't support Promises.
See .zuul.yml for the full list
of tested browsers. Assuming you have a Promise polyfill, the supported browsers should be:
Chrome
Firefox
Safari 8+
IE 10+
Edge
iOS 8+
Android 4.4+
If a browser doesn't support Web Workers but you still want to use this library,
then you can use pseudo-worker.
For Service Worker support, Chrome 40 and 41 are known to be buggy (see #9), but 42+ are supported.
This library is not designed to run in Node.js.
API
Main bundle
new PromiseWorker(worker)
Create a new PromiseWorker, using the given worker.
worker - the Worker or PseudoWorker to use.
PromiseWorker.postMessage(message)
Send a message to the worker and return a Promise.
message - object - required
The message to send.
returns a Promise
Worker bundle
Register a message handler inside of the worker. Your handler consumes a message
and returns a Promise or value.
registerPromiseWorker(function)
function
Takes a message, returns a Promise or a value.
Testing the library
First:
npm install
Then to test in Node (using an XHR/PseudoWorker shim):
Does nolanlawson/promise-worker have a project website?
No homepage URL was recorded for nolanlawson/promise-worker in TopGit's last sync. The README tab above frequently contains screenshots and demo links, or check the repository description on GitHub.
Does nolanlawson/promise-worker have any tags?
TopGit's last sync did not record any GitHub topics for nolanlawson/promise-worker. GitHub topics appear in the right sidebar of a repository page; that's the authoritative place to check.
How active is development on nolanlawson/promise-worker?
The most recent commit recorded on nolanlawson/promise-worker was 4.6 years ago, based on the GitHub push timestamp. The repository has 39 forks — one of the better signals of community interest.
How many stars does nolanlawson/promise-worker have?
nolanlawson/promise-worker has 482 GitHub stars — refresh the page for the live number, or check github.com/nolanlawson/promise-worker. TopGit mirrors GitHub's count but does not claim minute-by-minute accuracy.
What language is nolanlawson/promise-worker written in?
nolanlawson/promise-worker is written primarily in JavaScript. GitHub's language field is based on the largest share of bytes in the default branch.
What license does nolanlawson/promise-worker use?
nolanlawson/promise-worker is released under the Apache-2.0 license. Always verify the LICENSE file directly on GitHub for the authoritative terms — license strings can be edited out of sync with a project's actual stance.
Where do I read more about nolanlawson/promise-worker?
This TopGit page is a snapshot — the READ ME tab shows the project's own README content (links stripped, images preserved). The GitHub repository at github.com/nolanlawson/promise-worker is the definitive source.
Read full README in the tab above.
Is promise-worker worth your time?
ChatGPT, Claude and Perplexity can all read this page. Ask one of them what it makes of promise-worker.