Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

112
Vistas
Unable to clear array in async foreach

I am trying to process an unknown amount of items in an array and process them in another function in groups of 50.

So I created an array to batch the transactions up and did a foreach to iterate the items I need to process. As some of the cost is async, I am using async on the foreach function.

The problem I am running into is no matter what I do, the batch array once filled will stay at 50 items and not progress futher in the code. I have tried using batch = [], batch.length = 0, and slice. Nothing I do will allow me to empty the array once 50 items have been reached and processed so the next 50 can be processed.

Here is MVCE of what I am trying to do. I don't know if there is another pattern to accomplish this better.

async function doTheThing(users, batchSize = 50, ) {
  let batch = []

  users.forEach(async (user) => {
    if (batch.length === batchSize) {
      const data = JSON.stringify(allTransactions)
      await doSomethingCool()
      await sleep(delay)
      batch = []
    }

    if (user.balance) {
      batch.push(user)
    }
  })
}
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda