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…

const sleep = ms => {
  return new Promise(resolve => setTimeout(resolve, ms))
}

for(let i = 0;i <= 10;i++){
    await sleep(1000);
    console.log(new Date(), i);
}

Output

await, loop, Example

Thanks for reading…