https://github.com/chmoder/worker-promise-pool
Pool of Web Workers that return promises on message events
https://github.com/chmoder/worker-promise-pool
Last synced: about 1 year ago
JSON representation
Pool of Web Workers that return promises on message events
- Host: GitHub
- URL: https://github.com/chmoder/worker-promise-pool
- Owner: chmoder
- License: apache-2.0
- Created: 2016-08-06T21:43:19.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2018-12-08T17:00:56.000Z (over 7 years ago)
- Last Synced: 2025-04-19T22:59:41.322Z (about 1 year ago)
- Language: JavaScript
- Size: 33.2 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# worker-promise-pool
Pool of Web Workers that return promises on worker message events.
Create your pool by passing the number of workers you wouild like and the path to the worker script your workers will execute.
Then create a WorkerPromise, workload is the data the worker needs during execution.
Set onmessage to a callback you would like executed after your worker completes. Callback will have the data from the worker script if you put include it in the "postMessage" of your worker script.
[Try the example!](https://chmoder.github.io/worker-promise-pool/)
```javascript
// create a pool for working on WorkerPromise's with a given number of workers
var numberOfWorkers = 8;
var workerPool = new WorkerPool(numberOfWorkers, 'js/example_worker.js');
var callback = function(data) {
// do something with data from worker
}
var workerPromise = new WorkerPromise();
// args to pass to worker
workerPromise.workload = [];
// callback for when the worker completes its task
workerPromise.onmessage = callback;
// add WorkerPromise to the pool and queue it up for execution
workerPool.addWorkerPromise(workerPromise);
```