async await 实现

function fn2() {
     return new Promise((resolve,reject) => {
        setTimeout( () =>{
             resolve('李白')
        },2000)
     })
 } 
 async function fn1() {
     console.log( await fn2('sdx') )
     console.log(666)
 }
//第一组第一种调用
  console.log(fn1())

//第一组第二中调用 修改fn1()方法 then()方式调用
function fn1() {
    fn2().then(res => {
        console.log(res)
        console.log('梨花哈')
    })
}
fn1()

//为什么要使用 await
function fn2( unm ) {
   return new Promise((resolve,reject) => {
       setTimeout(() => {
           resolve(unm + 10)
       })
   })
}

async function fn1(){
   fn2(10).then(res1 => { //20
       fn2(20).then( res2 => { //30
           fn2(30).then( res3 => { //40
               console.log(res1 + res2 + res3)
           })
       })
   })
}
fn1()

//使用await
function fn2(num) {
   return new Promise( (resolve,reject) =>{
       setTimeout( () => {
           resolve(num + 10)
       })
   } )
}

async function fn1() {
    const res1 = await fn2(10)  // await执行完 在执行下面的函数
    const res2 = await fn2(20)
    const res3 = await fn2(30)
    console.log(res1 + res2 +res3)
}
fn1()

async 与 Promise

async fn() {
	console.log(1213)
}
function fn1() {
	return new Promise((resolve,reject) => {
	console.log(resolve(1213))
	})
}

console.log(fn() == fn1()) //全为 Promise形式
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值