Promises inside a loop - Javascript ES6

In this article, we will see how to write a promise function inside a loop. Promises inside a loop - Javascript ES6

If you want to learn more about Javascript. check out this articles

Prototypal Inheritance - Javascript Weekly

Understanding Closures in Javascript - Javascript Weekly

Configuring Babel for Node/Express

Before jumping into the code, we will see why we need to write a promise function inside a loop.

Scenario

Consider a situation where you need to send mail to list of emails. you will write a function which can send a single email at once.

Let's say that you have a list of emails like this

1const emailList = ["ganesh@gmail.com", "john@gmail.com", "tarry@gmail.com"]

Firstly, you write a single function which takes an emailId and send a mail to the corresponding mail.

1function sendMail() {
2 //Sending mail
3}

you need to call the function as per the count of emails you have in the array.

you can think of, why can't I call the function inside the for a loop. it will be simple ..right??.

No... that's not going to work. because sending a mail is an asynchronous function. Loop will not wait until the previous function completes

So, there might be a chance of missing a function call or sending mail. To solve this problem, we need to use Promise.

Promises inside a loop - Javascript ES6

Firstly, write the email sending logic inside a function which returns a Promise.

1const sendEmail = userEmail => {
2 return new Promise(async (resolve, reject) => {
3 //this is a mock email send logic
4 setTimeout(() => {
5 resolve(`Email Sent to ${userEmail}`)
6 }, 3000)
7 })
8}

call the function inside the Javascript map() function and Promise.all

1const sendEmails = async () => {
2 const userEmails = ["ganesh@gmail.com", "john@gmail.com", "Sam@gmail.com"]
3
4 const status = await Promise.all(userEmails.map(email => sendEmail(email)))
5
6 console.log("Status =>", status)
7}

This is one of the ways with which you can call promise inside loops.

To sum up, Promises are one the powerful addition in Javascript ES6.Promises inside a loop - Javascript ES6

In addition, we will see more about Core Concepts of Javascript in upcoming articles. Stay Tuned.

Until then Happy Coding :-)

Author

Hey, I’m Ganesh, Full stack engineer.I love to write technical content and help developers like me to grow in the industry. please consider supporting me.

To Read More

Building a Production-grade Nodejs,...

This article is the first part of building a production grade nodejs,graphql and...

Modern React Redux Tutorials with R...

This tutorial explain how you can build an application using modern react redux ...

Building a Piano with React Hooks

In this article, we will see how to build a piano with react hooks. Building a P...

TypeScript Basics - The Definitive ...

In this article, we will learn some basics of typescript which helps you to deve...

Here's why podman is more secured t...

In this article we will see about podman and why it is more secured way to run c...

Building a Production - Ready Node....

In this article, we will see how to build a Nodejs, TypeScript Application and d...

Nginx for Front-end Developers

This article is to explain Nginx for Front-end Developers in a much simpler way....

What is gRPC ? How to implement gRP...

Everyone talks about gRPC. Have you ever wonder how it works or how to implement...