async await Promise

2018-06-08  本文已影响7人  ymy1203

问题:await 一个 async 函数。async本身 await 了个Promise的异步函数

let obj = {
  a() {
    new Promise((resolve,reject)=>{
        console.log("1-1");
        resolve("1-2");
  }).then((value)=>{
        console.log(value);
  })},
  b(){
    new Promise((resolve,reject)=>{
        console.log("2-1");
        resolve("2-2");
  }).then((value)=>{
        console.log(value);
  })}
}

let c = async()=>{
  console.log("1-0");
  await obj.a();
  console.log("1-3");
}
let d = async()=>{
  console.log("2-0");
  await obj.b();
  console.log("2-3");
}

(async()=> {
  await Promise.all([c()])
  await Promise.all([d()])
  console.log(3);
})()

打印结果

1-0
1-1
1-2
1-3
2-0
2-1
2-2
2-3
3
上一篇 下一篇

猜你喜欢

热点阅读