controlled-promise-list

Run Async/Promise functions with controlled concurrency

Usage no npm install needed!

<script type="module">
  import controlledPromiseList from 'https://cdn.skypack.dev/controlled-promise-list';
</script>

README

Welcome to controlled-promise-list 👋

Version License: MIT

Iterate through a list of promises and run them with a controlled concurrency.

Install

yarn add controlled-promise-list

Or with NPM :

npm install controlled-promise-list

Usage

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.

Development

Setup

  1. Clone this repository
  2. yarn install

Run tests

yarn test

Author

👤 Ravidhu Dissanayake

Show your support

Give a ⭐️ if this project helped you!