I have an array of emails
emails.forEach(email => sendEmail(email)); --> 1
function2();
sendEmail is an asynchronous function I want function2 to be invoked only after line 1 gets completed
How can I achieve this?
I have an array of emails
emails.forEach(email => sendEmail(email)); --> 1
function2();
sendEmail is an asynchronous function I want function2 to be invoked only after line 1 gets completed
How can I achieve this?
How can I achieve this?
Don't use forEach
. Either use a normal for loop, and await
each call, or if you want to run them in parallel use map
and await Promise.all
.
My preference would be:
async function sendAllEmails(emails) {
await Promise.all(emails.map(email => sendEmail(email)));
function2();
}