vue:异步async and await

async 异步,会再最后执行

async function timeout() {
    return 'hello world'
}
console.log(timeout());
console.log('先执行');

如果async 函数中有返回一个值 ,当调用该函数时,内部会调用Promise.solve() 方法把它转化成一个promise 对象作为返回

async function timeout(flag) {
    if (flag) {
        return 'hello world'
    } else {
        throw 'my god, failure'
    }
}
console.log(timeout(true))  // 调用Promise.resolve() 返回promise 对象。
console.log(timeout(false)); // 调用Promise.reject() 返回promise 对象。

await

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

6秒后,输出220
必须等完成后才执行下一步

猜你喜欢

转载自blog.csdn.net/weixin_41143662/article/details/84303096