I have a multidimensional array of promises, but the execution of executeMethod occurs before the initial for loop ends and the code reaches the second for loop and Promise.all.
executeMethod is of course an async function.
const MAX_NUMBER_OF_CONCURRENT_PROMISES = 100;
const promisesArray: Promise<void>[][] = [];
let promiseArrayIndex = 0;
let innerPromiseArrayIndex = 0;
const objectsList = [......];
for (const bucketObject of objectsList) {
if (innerPromiseArrayIndex === MAX_NUMBER_OF_CONCURRENT_PROMISES) {
innerPromiseArrayIndex = 0;
promiseArrayIndex++;
promisesArray[promiseArrayIndex] = [];
}
promisesArray[promiseArrayIndex][innerPromiseArrayIndex] = (
executeMethod(bucketObject)
);
innerPromiseArrayIndex++;
}
for (let i=0; i< promiseArrayIndex; i++) {
await Promise.all(promisesArray[i]);
}
I want the executions to occur only after for (const bucketObject of objectsList) ends and I call Promise.all per each Promises array.
Please advise how can I resolve this?
It's not Promise.all that executes anything, it's the executeMethod(bucketObject) call. And those all happen synchronously in your loop, before you start waiting for anything.
To batch your executions, use
const MAX_NUMBER_OF_CONCURRENT_PROMISES = 100;
const objectsList = [......];
for (let index = 0; index<objectsList.length; index+=MAX_NUMBER_OF_CONCURRENT_PROMISES) {
const promisesArray: Promise<void>[] = objectsList.slice(index, index+MAX_NUMBER_OF_CONCURRENT_PROMISES).map(executeMethod);
await Promise.all(promisesArray);
}