Is there a way/pattern to implement let res = Promise.all([...p], limit)
?
res
should be resolved after all p
resolveslimit=3
Promises should run in parallel
limit
resolvers running in parallel.Especially the last point creates my headaches.
My current solution is to split the promises-array into chunks of limit
size and chain them. The disadvantages here is that the second bunch don't start until all Promises from bunch 1 has been resolved.
Assuming we have the processing power and that our promises can run in parallel, there is a hard limit of just over 2 million promises.
Promise.all() The Promise.all() method takes an iterable of promises as an input, and returns a single Promise that resolves to an array of the results of the input promises. This returned promise will fulfill when all of the input's promises have fulfilled, or if the input iterable contains no promises.
JavaScript's Promise. all() method takes in a parameter of iterable promises runs them concurrently then returns a single Promise that resolves to an array of results of the input promises.
Notice that it's not await that resolves them. Promise. all does not improve performance. It's the "not waiting for the first promise before starting the second task" that improves performance (if done correctly).
I came up with the idea of creating n = limit
chains which run in parallel and append as long as there are promises:
let promises = [];
for(let i=0; i<11; i++) promises[i] = () => {
console.log('Construct:',i);
return new Promise(resolve => {
setTimeout(function() {
console.log('Resolve:',i);
resolve(i);
}, Math.round(Math.random() * (2000 - 500) + 2000));
});
}
function parallelLimit(promiseFactories, limit) {
let result = [];
let cnt = 0;
function chain(promiseFactories) {
if(!promiseFactories.length) return;
let i = cnt++; // preserve order in result
return promiseFactories.shift()().then((res) => {
result[i] = res; // save result
return chain(promiseFactories); // append next promise
});
}
let arrChains = [];
while(limit-- > 0 && promiseFactories.length > 0) {
// create `limit` chains which run in parallel
arrChains.push(chain(promiseFactories));
}
// return when all arrChains are finished
return Promise.all(arrChains).then(() => result);
}
parallelLimit(promises, 4).then(console.log);
Excited to read your comments and suggestions :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With