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

Hello readers, Today In this article I am going to show you how to Solve – await is only valid in async function in nodejs. So here I will explain all the possible solutions.

Without wasting your time, Let’s start This Article to Solve This Error.

How await is only valid in async function in nodejs Error Occurs?

I have commonFun.js file and in this file, I have written this code.

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

and then I tried to use it in another file

var helper = require('./helper.js');   
 var start = function(a,b){
     ....
     const result = await helper.myfunction('test','test');
 }
 exports.start = start;

But I am facing the following error.

"await is only valid in async function"

So here are all the possible solutions that I tried to fix this error.

How To Solve await is only valid in async function in nodejs Error?

  • How To Solve await is only valid in async function in nodejs Error?

To Solve await is only valid in async function in nodejs Error Here The error is not referring to myfunction but to start. Here we wait for the myfunction to finish and then return a promise that’ll be waited for as well It’s useless to wait for the myfunction to finish before to return we can simply returns a promise that will be resolved later. Also, point that we don’t use the async keyword on the function because we can simply return the promise returned by myfunction.

  • await is only valid in async function in nodejs

To Solve await is only valid in async function in nodejs Error Here The error is not referring to myfunction but to start. Here we wait for the myfunction to finish and then return a promise that’ll be waited for as well It’s useless to wait for the myfunction to finish before to return we can simply returns a promise that will be resolved later. Also, point that we don’t use the async keyword on the function because we can simply return the promise returned by myfunction.

Solution 1

Here The error is not referring to myfunction but to start. Here we wait for the myfunction to finish and then return a promise that’ll be waited for as well It’s useless to wait for the myfunction to finish before to return we can simply returns a promise that will be resolved later. Also, point that we don’t use the async keyword on the function because we can simply return the promise returned by myfunction.

async function myfunction() {
  console.log('Inside of myfunction');
}

function start() {
  return myfunction();
}

// Call start
(async() => {
  console.log('before start');

  await start();
  
  console.log('after start');
})();

Solution 2

this error message was actually referring to the map function not being marked as “async”. I got around this issue by taking the “await” call out of the map function and coming up with some other way of getting the expected behavior.

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

Now, I hope your error will be gone.

Conclusion

In this article, we have discussed what causes the error and we have discussed ways to fix the error.

we hope this article has been informative. Thank you for reading. Kindly comment and let us know if you found it helpful.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Categories