Navigate back to the homepage

How to use await in javascript for loop?

Infinitbility
JavaScript
December 3rd, 2021 · 1 min read
How to use await in javascript for loop?

Hello Friends 👋,

Welcome To Infinitbility! ❤️

Here, I’m come to share my favorite syntax to use await in for loop, many folks think about how can we use await in for loop or how to write async for a loop.

First, you don’t need to make for loop async, for loop are able to handle await without defining async.

Now, the question comes how can we use await in for loop.

take the below example to use await in for loop, it will console next I value after 1 second…

1const sleep = ms => {
2 return new Promise(resolve => setTimeout(resolve, ms))
3}
4
5for(let i = 0;i <= 10;i++){
6 await sleep(1000);
7 console.log(new Date(), i);
8}

Output

await, loop, Example

Thanks for reading…

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo

Tutorials

Categories

More articles from Infinitbility

SQLite insert or update statement example

SQLite insert or update statement example

SQLite upserts or insert or replace query example

December 2nd, 2021 · 1 min read
How to create SQLite database at runtime in react native?

How to create SQLite database at runtime in react native?

React Native sqlite runtime database setup example

December 1st, 2021 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]