await is only valid in async function

后端 未结 9 1450
青春惊慌失措
青春惊慌失措 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:14

    I had the same problem and the following block of code was giving the same error message:

    repositories.forEach( repo => {
            const commits = await getCommits(repo);
            displayCommit(commits);
    });
    

    The problem is that the method getCommits() was async but I was passing it the argument repo which was also produced by a Promise. So, I had to add the word async to it like this: async(repo) and it started working:

    repositories.forEach( async(repo) => {
            const commits = await getCommits(repo);
            displayCommit(commits);
    });
    

提交回复
热议问题