js async和await的用法(ES7)

js async和await的用法(ES7)

建议:阅读本篇需要对Promise有一定的认识

1.含义和用法

async 是一个修饰符,被它定义的函数会默认的返回一个 Promise 的 resolve的值。
因此对 async 函数可以直接进行 then 操作,返回的值即为 then() 方法的传入函数。

// demo
async function demo_1() {
    console.log('a')
    return 1
}
demo_1().then(res => { 
  console.log(res)   //  a, 1,
})  

await同 async 一样,作为修饰符,但是它只能放在 async 内部使用。
它是获取 Promise 中返回的内容, 即这个 Promise 函数中 resolve 的值。

所以,async 用于申明一个 function 是异步的,而 await 用于等待一个异步方法执行完成。
如下例:

const myFun = function(){ return 'test'}

async function demo_2() {
   const a = await 1
   const b = await new Promise((resolve, reject)=>{
        setTimeout(function(){
           resolve('wait')
        }, 3000)
   })
   const c = await myFun()
   console.log(a, b, c)
}

demo_2()
/* 打印结果:
  3秒后输出:  1,"wait" ,"test",
 */

2.应用

比如说,这样一个场景:等待三个数据结果都返回,计算它们的和

const myFun2 = function(val, time) {
    return new Promise((resolve, reject) =>{
        setTimeout(()=>{
            resolve(val)
        }, time)
    })
}

const demo_3 = async function() {
    let a = await myFun2(3, 5000)
    console.log(a)
    let b = await myFun2(4, 10000)
    console.log(b)
    let c =  await myFun2(5, 15000)
    console.log(c)
    let d = a + b +c  
    console.log(d)
}

demo_3()
/* 打印结果:
  5秒后输出:  3
  10秒后输出:  4
  15秒后输出:  5 12
 */
async function demo() {

    const a = await new Promise((resolve, reject) => {
        setTimeout(() => {
            let flag = Math.random() > 0.5 ? true : false
            if (flag) {
                resolve("事件为真")
                return
            }
            reject("事件为假")
        }, 3000)

    }).catch(err=>console.log("事件为假"))
    if(!a){
        console.log("a没有被正确赋值,返回")
        return
    }
    console.log("a=",a)

}
demo()

3.async函数的返回值

async函数的返回值也是一个promise对象,需要使用.then来接收参数

async function helloAsync() {
    const result = await new Promise((resolve) =>
        setTimeout(() => resolve("Hello"))
    );
    return result;
}

helloAsync().then((data) => {
    console.log("helloAsync返回值:" + JSON.stringify(data));
});

或者再使用一个async函数来接收返回值

async function helloAsync() {
    const result = await new Promise((resolve) =>
        setTimeout(() => resolve("Hello"))
    );
    return result;
}
async function testHelloAsync() {
    const output = await helloAsync();
    console.log("helloAsync返回值:" + JSON.stringify(output));
}

testHelloAsync();
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值