p-gather
v1.0.2
Published
Execute Promise in parallel with concurrency support and gather all the results
Downloads
25
Readme
p-gather
Execute Promise in parallel with concurrency support and gather all the results.
p-gather
is similar with co-parallel, but p-gather
will gather all the result of these Promise, even those Promise throw error.
Installation
$ npm install p-gather
Example
const gather = require('p-gather');
const thread = gather.thread;
function sleep(n) {
return new Promise(resolve => {
setTimeout(resolve, n);
});
}
let index = 0;
async function random() {
let i = index++;
await sleep(Math.random() * 100);
if (Math.random() > 0.5) {
throw new Error('error');
}
return i;
}
(async =>{
const ret = await gather(thread(random, 10));
console.log(ret);
})();
=>
[
{ isError: true, error: [Error: error] },
{ isError: true, error: [Error: error] },
{ isError: true, error: [Error: error] },
{ isError: true, error: [Error: error] },
{ isError: true, error: [Error: error] },
{ isError: false, value: 5 },
{ isError: false, value: 6 },
{ isError: false, value: 7 },
{ isError: true, error: [Error: error] },
{ isError: true, error: [Error: error] }
]
API
gather(Promise, [concurrency])
Execute Promise
array in parallel, with the given concurrency defaulting to 5, and gather the result
gather.thread(Function, [concurrency])
Run a function in parallel N times. Function must return a Promise Object.
License
MIT