I want to process batches of data in series, like shown in the code below. In this context a single batch is simply an array containing values. So the function sendInBatches() expects an array of arrays as input.
async sendInBatches(batches) {
for (const batch of batches) {
const promises = batch.map(x => asyncMethod(x));
await Promise.all(promises);
}
}
Below is the code for asyncMethod(). Note that the asyncMethod() doesnt actually do anything with the provided argument yet. It simply returns a Promise that resolves after 1 second.
asyncMethod(batch){
return new Promise((resolve) => {
setTimeout(
() => {
console.log('x');
resolve();
}
, 1000,
);
});
}
I try running the code like this:
sendInBatches([[1,2,3],[4,5,6],[7,8,9]]).then(console.log('done'));
This provides the output:
done
x
x
x
While I want it to return:
x
x
x
done
I cant figure out whats going wrong here, do you guys have an idea?