close

[Fixed] await is only valid in async function in nodejs

Today We are Going To Solve await is only valid in async function in nodejs in nodejs. Here we will Discuss All Possible Solutions and How this error Occurs So let’s get started with this Article.

How to Fix await is only valid in async function in nodejs Error?

  1. How to Fix await is only valid in async function in nodejs Error?

    To Fix await is only valid in async function in nodejs Error just Use the code. Here to solve this error you have to just use the below code. here I am taking the “await” call out of the map function It will help you. var myfunction = async function(x,y) { .... someArray.map(someVariable => { // <- This was the function giving the error return await someFunction(someVariable); }); }

  2. await is only valid in async function in nodejs

    To Fix await is only valid in async function in nodejs Error just Use repo arguments. To solve the error just pass the repo argument just like the below code and it will help you. repositories.forEach( async(repo) => { const commits = await getCommits(repo); displayCommit(commits); });

Solution 1 : Use the code

Here to solve this error you have to just use the below code. here I am taking the “await” call out of the map function It will help you.

var myfunction = async function(x,y) {
    ....
    someArray.map(someVariable => { // <- This was the function giving the error
        return await someFunction(someVariable);
    });
}

Solution 2 : Use repo arguments

To solve the error just pass the repo argument just like the below code and it will help you.

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

Conclusion

So these were all possible solutions to this error. I hope your error has been solved by this article. In the comments, tell us which solution worked? If you liked our article, please share it on your social media and comment on your suggestions. Thank you.

Also Read These Solutions

Leave a Comment