美文网首页
async await Promise

async await Promise

作者: ymy1203 | 来源:发表于2018-06-08 11:19 被阅读7次
  • 深入理解 async await Promise Promise.all()

问题: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

相关文章

网友评论

      本文标题:async await Promise

      本文链接:https://www.haomeiwen.com/subject/joiosftx.html