Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/Questions/syntaxerror: await is only valid in async function - How to fix?
Next
Answered
Kennedy Koss
  • 0
Kennedy Koss
Asked: August 10, 20222022-08-10T17:01:57+00:00 2022-08-10T17:01:57+00:00In: Error

syntaxerror: await is only valid in async function – How to fix?

  • 0

I am writing my program, then error syntaxerror: await is only valid in async function shows up.

This is my code in commonFun.js file:

var myfunction = async function(x,y) {

....
return [variableA, variableB]
}
exports.myfunction = myfunction;

After using it in a different file

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

I got error:

"await is only valid in async function"

Tell me the way to solve this problem, please.

Thanks a lots.

 

  • 1 1 Answer
  • 46 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    dttutoria Expert
    2022-08-10T17:40:27+00:00Added an answer on August 10, 2022 at 5:40 pm

    Solution:

    Solution 1.

    The issue refers to start rather than myfunction. Here, you wait for the myfunction to complete before returning a promise that will also be awaited. Point out that you may return the promise that myfunction returned instead of using the async hashtag on the function.

    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 actually referred to the map function’s lack of a “async” designation. By removing the “await” call from the map function and finding another way to achieve the desired behavior, the syntaxerror: await is only valid in async function can be fixed.

    var myfunction = async function(x,y) {
    ....
    someArray.map(someVariable => { // <- This was the function giving the error
    return await someFunction(someVariable);
    });
    }
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.