Why does async/await in a forEach loop not wait, and how do I process items sequentially?
intermediate
· 6 views ·
asked 3 days, 11 hours ago
by Tai
0
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.
Question comments
Short clarifications and small follow-ups about the question itself.
No comments on the question yet.
0 Answers
No answers yet. Be the first to help!