async...await

另一个解决回调函数的方案

async function foo(){
  return 'hello'
}


console.log(foo()) // 返回了promise对象

//既然是promise对象所以可以使用then方法

foo().then((res) => {
  console.log(res) // hello
})


// await 只能在 async的函数里,它可以等待异步的对象, 这是另一个解决回调地狱的方案
async function bar(){
  let data = await new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(5555)
    }, 1000);
  })

  console.log(data) // 555
}
bar()

原文地址:https://www.cnblogs.com/tourey-fatty/p/12721484.html