Execute Promise In Synchronous Batches Javascript
I have a promise function that receives a row from rows array to a remote server. const post = (row) => new Promise(resolve=> { //do post then, resolve(respon
Solution 1:
can use Promise.all
for the batch and await
for resolve:
constpost = (row) => newPromise(resolve => {
setTimeout(
() =>resolve(row.id), //for demo purpose1000);
})
const rows = [{
id: 1
}, {
id: 2
}, {
id: 3
}, {
id: 4
}, {
id: 5
}, {
id: 6
}, {
id: 7
}];
const execute = async (batchSize) => {
let currentPtr = 0;
while (currentPtr < rows.length) {
const results = awaitPromise.all(
rows.slice(currentPtr, currentPtr + batchSize)
.map(row =>post(row))
)
console.log(results);
currentPtr += batchSize;
}
}
execute(2);
Post a Comment for "Execute Promise In Synchronous Batches Javascript"