Tengo una matriz multidimensional de promesas, pero la ejecución de executeMethod ocurre antes de que finalice el bucle for inicial y el código llega al segundo bucle for y Promise.all .
executeMethod es, por supuesto, una función asíncrona.
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]); } Quiero que las ejecuciones ocurran solo después for (const bucketObject of objectsList) y llamo a Promise.all por cada matriz Promises.
Por favor, indique cómo puedo resolver esto.
No es Promise.all lo que ejecuta nada, es la executeMethod(bucketObject) . Y todo eso sucede sincrónicamente en su ciclo, antes de que comience a esperar algo.
Para procesar por lotes sus ejecuciones, 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); }