async与await

async 作为一个关键字,用于声明一个函数为 异步函数。与普通函数的唯一区别,就是 是异步执行,不会影响代码的继续执行

当函数返回一个值时,内部会调用 Promise.resolve(该值),返回一个 promise。想要 reject ,可使用 throw 抛出错误。

await 等待,后面可以跟任何表达式,大多情形跟的是一个返回promise 的表达式。只能放在 async里。
在async函数里, 可写多个await, 处理多个异步请求,按顺序 能拿到值,依次处理逻辑。

async function testResult() {
    let first = await doubleAfter2seconds(30);
    let second = await doubleAfter2seconds(50);
    let third = await doubleAfter2seconds(30);
    console.log(first + second + third);
}


参考链接:https://www.cnblogs.com/SamWeb/p/8417940.html

posted @ 2019-05-05 10:52  点几  阅读(582)  评论(0编辑  收藏  举报