Why does async/await in a forEach loop not wait, and how do I process items sequentially?
0votes
Context
Node.js script that processes an array of ~50 records and calls an external API for each one:
records.forEach(async (r) => {
await sendToApi(r);
});
console.log('done');
Expected
Each record should be sent one at a time, and 'done' should only print after all API calls have completed.
What Happened
'done' prints immediately, all 50 requests fire at once, and the API starts returning 429 rate-limit errors.
No comments yet.