Iterate through a list of promises and run them with a controlled concurrency.
yarn add controlled-promise-list
Or with NPM :
npm install controlled-promise-list
import controlledPromiseList from 'controlled-promise-list';
const data = [ 1, 2, 3, 4, 5 ];
// Number of promises that run at the same time,
// here only 2 promises will run concurrently.
const concurrentRunNumber = 2;
// The list contain promise functions ready to be use for a Promise instantiation.
const promiseFunctionList = data.map((x) => {
return (resolve, reject) => {
setTimeout(() => {
resolve(x * x)
}, between(1000, 3000))
}
});
// This function is run after each batch of promises,
// here it's run each time when 2 promises finish.
const onProgress = (doneCount, remainingCount) => {
console.log(doneCount + '/' +remainingCount);
}
controlledPromiseList(
promiseFunctionList,
concurrentRunNumber,
onProgress
)
.then((results) => {
console.log(results); // [ 1, 4, 9, 16, 25 ]
})
.catch((error) => {
// if one of the function in promiseFunctionList reject,
// controlledPromiseList stops and throw the error so it can be catch.
});
Notice : if you set concurrentRunNumber
to 1, it will run each promise sequentially.
- Clone this repository
yarn install
yarn test
👤 Ravidhu Dissanayake
Give a ⭐️ if this project helped you!