Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

111
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda