Babel 6 regeneratorRuntime is not defined

前端 未结 30 1861
暖寄归人
暖寄归人 2020-11-22 03:49

I\'m trying to use async, await from scratch on Babel 6, but I\'m getting regeneratorRuntime is not defined.

.babelrc file

{
    \"presets\": [ \"es2         


        
30条回答
  •  情深已故
    2020-11-22 04:24

    Be careful of hoisted functions

    I had both my 'polyfill import' and my 'async function' in the same file, however I was using the function syntax that hoists it above the polyfill which would give me the ReferenceError: regeneratorRuntime is not defined error.

    Change this code

    import "babel-polyfill"
    async function myFunc(){ }
    

    to this

    import "babel-polyfill"
    var myFunc = async function(){}
    

    to prevent it being hoisted above the polyfill import.

提交回复
热议问题