Categories
JavaScript Answers

How to write loops for promise with JavaScript?

Spread the love

To write loops for promise with JavaScript, we use async and await.

For instance, we write

const myFunction = async () => {
  //...
  while (condition) {
    const res = await db.getUser(email);
    logger.log(res);
  }
};

to use a while loop in the myFunction function.

In it, we use await to wait for the promise to resolve and return the value before we move to the next iteration.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *