Skip to content

Instantly share code, notes, and snippets.

@Tribhuwan-Joshi
Created May 18, 2023 11:33
Show Gist options
  • Save Tribhuwan-Joshi/99800c0308c3fc9ccaeae95a57a71957 to your computer and use it in GitHub Desktop.
Save Tribhuwan-Joshi/99800c0308c3fc9ccaeae95a57a71957 to your computer and use it in GitHub Desktop.
Promise pool to limit concurrency
const promisePool = async function(functions, n) {
async function evaluateNext(){
if(functions.length == 0 ) return ;
const fn = functions.shift();
await fn();
await evaluateNext(); // call the function recursively until the function ends
}
const nPromises = Array(n).fill().map(evaluateNext); // create array of size n to call the evalute next fn.
await Promise.all(nPromises); // wait for all promise to resolve
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment