Working With ECMAScript 2019 Asynchronous Iteration Using “for-of”

How to use “for-of” loops in ECMAScript 2019

Mayank Gupta
Aug 2, 2019 · 4 min read
Image for post
Image for post

Creating a Simple Promise Using “New Promise”

The code below gives a simple way to create a Promise in JavaScript:


Iterating on Array of Promises With “for-of”

Now, let's assume the situation is we have an Array of Promises, and the user then iterates through the list of Promises using the “for-each” loop. What do you expect to be logged during each iteration? Let’s see with the help of this example:

Image for post
Image for post

Returning Resolved Value on Iterating With “for-of”

The above iteration logs each Promise Object to the user. We would further like to enhance the functionality so that, while iterating through the list of Promises, the value logged would be the resolved output from each Promise, rather than the Promise itself.

Image for post
Image for post

The Order of Promises in the Array

The other important thing that needs to be considered is that the order of output is the same as the order of Promises in the Array. Let's see this in action with the help of another example:

Image for post
Image for post

Conclusion

This is a nice, cool feature to work with. Do find places in your code where you can make use of this feature.

TechnoFunnel

Get going with Latest Technology Trends

Sign up for TechnoFunnel

By TechnoFunnel

Getting stared with Latest Technologies. Connect with us for more detailed articles and in-depth explanation of many frontend and backend Technologies and concepts Take a look

Create a free Medium account to get TechnoFunnel in your inbox.

Mayank Gupta

Written by

9 Years of Experience with Front-end Technologies and MEAN Stack. Working on all Major UI Frameworks like React, Angular and Vue.

TechnoFunnel

Articles for Multiple Technologies

Mayank Gupta

Written by

9 Years of Experience with Front-end Technologies and MEAN Stack. Working on all Major UI Frameworks like React, Angular and Vue.

TechnoFunnel

Articles for Multiple Technologies

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store