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)
}
})
}