/run-each-limit

Run an async task for each array element in parallel, but limit the number of tasks executing at the same time.

Primary LanguageJavaScript

NPM version Build Status Dependency Status

run-each-limit

Run an async task for each array element in parallel, but limit the number of tasks executing at the same time.

Install

$ npm install --save run-each-limit

Usage

Callback

import eachLimit from 'run-each-limit';

const items = ['a', 'b', 'c', 'd'];
const result = '';

function onItem(item, fn) {
  result += item;
  setTimeout(fn, 200);
}

eachLimit(items, 2, onItem, function(err) {
  console.log(result); // 'abcd'
});

Promise

import eachLimit from 'run-each-limit/promises';

const items = ['a', 'b', 'c', 'd'];
const result = '';

function onItem(item) {
  result += item;
  return new Promise(resolve => setTimeout(resolve, 200));
}

eachLimit(items, 2, onItem).then(() => {
  console.log(result); // 'abcd'
});

License

MIT © Damian Krzeminski