Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

110
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!