можно использовать Promise.all
для партии и await
для разрешения:
const post = (row) => new Promise(resolve => {
setTimeout(
() => resolve(row.id), //for demo purpose
1000);
})
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 = await Promise.all(
rows.slice(currentPtr, currentPtr + batchSize)
.map(row => post(row))
)
console.log(results);
currentPtr += batchSize;
}
}
execute(2);