1

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)
    }
  })
}
Michael
  • 525
  • 1
  • 8
  • 20
  • 1
    async/await in forEach never works like you expect ... – Bravo Aug 28 '21 at 08:14
  • 1
    Just use a `for-of `loop. `for-of` loops work well in `async` functions when you want to do things serially. `forEach` doesn't, because it doesn't wait for the previous callback to settle its promise before calling the next one. – T.J. Crowder Aug 28 '21 at 08:17
  • It all worked great once I switched it to a for of, thank you. Anywhere I can read exactly why it acts like this? – Michael Aug 28 '21 at 09:29

0 Answers0