await is only valid in async function

后端 未结 9 1457
青春惊慌失措
青春惊慌失措 2020-11-22 06:53

I wrote this code in lib/helper.js

var myfunction = async function(x,y) {
   ....
   return [variableA, variableB]
}
exports.myfunction = myfunct         


        
9条回答
  •  孤街浪徒
    2020-11-22 07:35

    If you are writing a Chrome Extension and you get this error for your code at root, you can fix it using the following "workaround":

    async function run() {
        // Your async code here
        const beers = await fetch("https://api.punkapi.com/v2/beers");
    }
    
    run();
    

    Basically you have to wrap your async code in an async function and then call the function without awaiting it.

提交回复
热议问题