parallomise
v0.3.1
Published
Run some ecma6 promises in parallel
Downloads
4
Readme
Parallomise
Run native javascript promises in parallel.
Requirements
Installation
npm install --save parallomise
Usage
new Parallomise(options)
Construct a new Parallomise.
options
maxConcurrents
- N maximum tasks may run in parallel. Defaults to10
.
.add(fn, id)
Add a function to the queue. Returns the id.
fn
- a function that returns a promiseid
- identifier for this task, broadcasted inpromiseFulfilled
andpromiseRejected
events.
.on(evt, callback)
Extends event emitter. Events/callbacks are:
promiseFulfilled
- emitted when a promise (one task) is fulfilledcallback(id, result)
id
- id of taskresult
- result passed from promise resolver
promiseRejected
- emitted when a promise (one task) is rejectedcallback(id, error)
id
- id of taskerror
- error passed from promise rejector
.run()
Run the suite of tasks. Returns a promise that, after all promises complete, is fulfilled with a results
map, keys being task ids and values are objects like so:
fulfilled
: true/false, whether the promise was fulfilledrejected
: true/false, whether the promise was rejected
Values are not passed to avoid memory leakage. Instead, listen for the promiseFulfilled event
Results are added in the order that the tasks were originally added to the parallomise.
.clear()
Clears/resets this parallomise instance. Cannot be called when this parallomise is running.
Example
let Parallomise = require('parallomise'),
parallomise = new Parallomise();
function longIOOperation(a) {
// returns a promise
}
parallomise.add(longIOOperation.bind(null,1), 'op1');
parallomise.add(longIOOperation.bind(null,2), 'op2');
parallomise.add(longIOOperation.bind(null,3), 'op3');
parallomise.on('promiseFulfilled', (id, result) => {
console.log('Finished ' + id + ' with result ' + result);
});
parallomise.on('promiseRejected', (id, e) => {
console.log('Error in ' + id + ', ' + e.stack);
});
parallomise
.run()
.then(resultsIterator => {
for (let result of resultsIterator) {
console.log(result);
/*
{
fulfilled: true
rejected: false
}
{
fulfilled: true
rejected: false
}
{
fulfilled: true
rejected: false
}
*/
}
});
Testing
grunt test