close

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

Hello Guys, How are you all? Hope You all Are Fine. Today I am running my node js code but I am facing the following error in my console await is only valid in async function in nodejs. So Here I am Explain to you all the possible solutions here.

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('./commonFun.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"

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

  1. 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 return 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.

  2. 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);
    });
}

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you?

Also Read

Please Rate Our Solution to Motivate Us For Give Our Best

Leave a Comment