Async/Await报错

这段代码问题在哪,一运行就报错

    var sleep = async function(para) {

        return new Promise(function(resolve, reject) {

            setTimeout(function() {

                resolve(para * para)

            }, 1000)

        })

    }

    var errorSleep =async function(para) {

        return new Promise(function(resolve, reject) {

            setTimeout(function() {

                reject(' ErrorSleep')

            }, 1000)

        })

    }

    try {

        var result1 = await sleep(1);

        var result2 = await errorSleep(4);

        var result3 = await sleep(1);

        console.log('result1: ', result1)

        console.log('result2: ', result2)

        console.log('result3: ', result3)

    } catch (err) {

        console.log('err: ', err)

        console.log('result1: ', result1)

        console.log('result2: ', result2)

        console.log('result3: ', result3)

    }

    

    

https://img.mukewang.com/5c1dcdd80001e40405220351.jpg

慕妹3146593
浏览 572回答 1
1回答

蝴蝶不菲

await 只能在 async 包装的函数里面用。就和yield只能在generator函数里面用一样。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript