Why does async/await in a forEach loop not wait, and how do I process items sequentially?

0votes

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');

Each record should be sent one at a time, and 'done' should only print after all API calls have completed.

'done' prints immediately, all 50 requests fire at once, and the API starts returning 429 rate-limit errors.

Question comments

Short clarifications and follow-ups about the question itself.

No comments yet.

0 Answers

No answers yet

There are no published answers to show.

Your answer

Sign in to share a solution.