Why await can't be used from a normal function or the global scope

前端 未结 1 1219
南笙
南笙 2021-01-28 08:37

I was reading a blog that have the below sample code,

async function fAsync() {
    // actual return value is Promise.resolve(5)
    return 5;
}

// can\'t call          


        
相关标签:
1条回答
  • 2021-01-28 09:13

    A "top level await" is currently at stage 2 (draft) on TC39's ECMAScript standard

    https://github.com/tc39/proposal-top-level-await

    It has been at stage 2 for 3 years, so, I don't know if it will ever make it to stage 3 (candidate) let alone stage 4 (finished)


    EDIT

    Original answer was in November 2018. In June 2019 top level await graduated to stage 3 (candidate). Stage 4 is very likely to follow, but hard to say when.

    Side note : If you want to dive deeper, this gist and some of its comments are informative on the subject.

    0 讨论(0)
提交回复
热议问题