Skip to content

Latest commit

 

History

History
79 lines (58 loc) · 1.54 KB

README.md

File metadata and controls

79 lines (58 loc) · 1.54 KB

co-gather

Execute thunks, generators, async functions in parallel with concurrency support and gather all the results.

co-gather is similar with co-parallel, but co-gather will gather all the result of these thunks, even those thunks throw error.

Installation

$ npm install co-gather

Example

  • generator function
const gather = require('./');
const sleep = require('mz-modules/sleep');

function* fun(result, error, interval) {
  yield sleep(interval || 100);
  if (error) throw new Error(error);
  return result;
}

console.time('gather');
gather([
  fun(1),
  fun(null, 'error'),
], 2).then(res => {
  console.timeEnd('gather');
  console.log(res);
});
  • async function

Notice: You must pass async functions or functions return promise. If a promise is passed, it will start executing when it's created, and there's no way to control concurrency through gather.

const gather = require('./');
const sleep = require('mz-modules/sleep');

async function fun(result, error, interval) {
  await sleep(interval || 100);
  if (error) throw new Error(error);
  return result;
}

console.time('gather');
gather([
  () => fun(1),
  async () => fun(null, 'error'),
], 2).then(res => {
  console.timeEnd('gather');
  console.log(res);
});

=>

[
  { isError: false, value: 1 },
  { isError: true, error: [Error: error] }
]

API

gather(items, [concurrency])

Execute items in parallel, with the given concurrency defaulting to 5, and gather the result

License

MIT