Correct way to write loops for promise.

后端 未结 13 1735
猫巷女王i
猫巷女王i 2020-11-22 15:20

How to correctly construct a loop to make sure the following promise call and the chained logger.log(res) runs synchronously through iterat

13条回答
  •  花落未央
    2020-11-22 15:47

    There is a new way to solve this and it's by using async/await.

    async function myFunction() {
      while(/* my condition */) {
        const res = await db.getUser(email);
        logger.log(res);
      }
    }
    
    myFunction().then(() => {
      /* do other stuff */
    })
    

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function https://ponyfoo.com/articles/understanding-javascript-async-await

提交回复
热议问题