Promise的原理和使用,以及async和await的使用

function a(){
return 1;
}

function b(){
return 2;
}

function c(){
return 2;
}



async function aa(){
return 1;
}

使用await

async function do(){
  const  ap = Promise.resolve(3);  
  ap.then(res=>{
  });

  const dd = awaut ap;
  console.log('dd',dd);
//或者

  const ap = await 4;  ==> 等于 Promise.resolve(3);
console.log('dd',dd);

//或者
  const ap = await aa();
}

解决Pormise的回调地狱

以下是在层层调用的基础上 a=>b(需要a的数据)=>c(需要b的数据)

function do(){
    a().then(res=>{
        console.log(a);
        return b()
}).then(res=>{
        console.log(b);
        return c()
}).then(res=>{
       console.log(c);
});
}

Promise.all 的使用

function do(){
const aPromise = a()
const bPromise = b()
Promise.all([aPromise, bPromise]).then(res=>{
        console.log('res', res); 
        console.log('等待上面的两个全部完成后才执行');
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值