The "for await of" loop syntax enables you to loop through an Array of Promises and await each of the responses asynchronously. This lesson walks you through creating the array and awaiting each of the results.

 

let promises = [
    Promise.resolve(1),
    Promise.resolve(2),
    new Promise(resolve => {
        setTimeout(() => {
            resolve(3)
        }, 3000)
    })
]

async function start() {
    for await (let promise of promises) {
        console.log(promise)
    }
}

start()

 

It will log out

1

2

At once..

 

Then after 3 seconds, log 

3

相关文章:

  • 2021-03-07
  • 2021-07-10
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-25
  • 2021-12-18
  • 2022-02-21
猜你喜欢
  • 2021-06-04
  • 2021-07-02
  • 2022-02-16
  • 2022-01-23
  • 2022-02-01
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案