Created
May 18, 2023 11:33
-
-
Save Tribhuwan-Joshi/99800c0308c3fc9ccaeae95a57a71957 to your computer and use it in GitHub Desktop.
Promise pool to limit concurrency
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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