ES6 - async、await

2020-03-19  本文已影响0人  coderfl
function ysz() {
    return new Promise((resolve, reject) => {
        let sino = parseInt(Math.random() * 6 + 1)
        setTimeout(() => {
            resolve(sino)
        }, 3000)
    })
}

async function test() {
    let n = await ysz();
    console.log(n);  // 如果不用async和await,这块的log会先执行输出Promise { <pending> }
}

test();
function nn(){
    return new Promise(resolve => {
        setTimeout(function () {
            resolve(22)
        },1000)
    })
}

async function fn() {
    console.log(11)
    await nn().then(value => console.log(value))
    console.log(33)

}

fn()  // 11    22    33

可参考博客:阮一峰 async 函数的含义和用法

上一篇 下一篇

猜你喜欢

热点阅读