372

I wrote this code in lib/helper.js:

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

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;

I got an error:

await is only valid in async function

What is the issue?

6
  • 10
    Well, the issue is that await can only be used inside an async function. That is, await makes a function asynchronous, so it must be declared as such.
    – Pointy
    Commented Mar 22, 2018 at 15:30
  • What is the current error?
    – acdcjunior
    Commented Mar 22, 2018 at 15:43
  • 1
    still the same, SyntaxError: await is only valid in async function
    – j.doe
    Commented Mar 22, 2018 at 15:45
  • You need to share more context about your code.
    – Ele
    Commented Mar 22, 2018 at 15:54
  • 2
    Possible duplicate of How do I return the response from an asynchronous call?
    – Liam
    Commented Mar 22, 2018 at 16:02

15 Answers 15

427

The error is not refering to myfunction but to start.

async function start() {
   ....

   const result = await helper.myfunction('test', 'test');
}

// My function
const myfunction = async function(x, y) {
  return [
    x,
    y,
  ];
}

// Start function
const start = async function(a, b) {
  const result = await myfunction('test', 'test');
  
  console.log(result);
}

// Call start
start();



I use the opportunity of this question to advise you about an known anti pattern using await which is : return await.


WRONG

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

// Here we wait for the myfunction to finish
// and then returns a promise that'll be waited for as well
// It's useless to wait the myfunction to finish before to return
// we can simply returns a promise that will be resolved later

// useless async here
async function start() {
  // useless await here
  return await myfunction();
}

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

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


CORRECT

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

// Here we wait for the myfunction to finish
// and then returns a promise that'll be waited for as well
// It's useless to wait 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 async keyword on the function because
// we can simply returns the promise returned by myfunction
function start() {
  return myfunction();
}

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

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


Also, know that there is a special case where return await is correct and important : (using try/catch)

Are there performance concerns with `return await`?

6
  • But this is not working, I updated my code. I still get the same error
    – j.doe
    Commented Mar 22, 2018 at 15:35
  • @j.doe I've added a snippet
    – Orelsanpls
    Commented Mar 22, 2018 at 15:53
  • 2
    Thanks, I found my problem. I was trying to do it inside a callback is the start() function. The solution was : const start = async function(a, b) { task.get(options, async function (error, result1) { const result = await myfunction('test', 'test');
    – j.doe
    Commented Mar 22, 2018 at 16:00
  • Considering that Node is a single threaded. Doesn't it decreases the request per minutes and also increases the delay in between fullfilling requests. Commented Sep 8, 2019 at 15:40
  • 1
    It's worth mentioning that in the "CORRECT" example, it isn't necessary to declare start as an async function (although some will chose to do so anyways, in order to be more explicit) Commented Jan 7, 2020 at 20:02
41

To use await, its executing context needs to be async in nature

As it said, you need to define the nature of your executing context where you are willing to await a task before anything.

Just put async before the fn declaration in which your async task will execute.

var start = async function(a, b) { 
  // Your async task will execute with await
  await foo()
  console.log('I will execute after foo get either resolved/rejected')
}

Explanation:

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

Wondering what's going under the hood

await consumes promise/future / task-returning methods/functions and async marks a method/function as capable of using await.

Also if you are familiar with promises, await is actually doing the same process of promise/resolve. Creating a chain of promise and executes your next task in resolve callback.

For more info you can refer to MDN DOCS.

3
  • 2
    Even with async in the start function I am getting the error
    – j.doe
    Commented Mar 22, 2018 at 15:49
  • I am not sure where you are missing and getting this error, there is no such complex explanation to resolve this error. Commented Mar 22, 2018 at 15:58
  • this is a proper answer and actually explained the underline reason. up voted.
    – linehrr
    Commented Oct 10, 2019 at 19:54
17

When I got this error, it turned out I had a call to the map function inside my "async" function, so 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);
    });
}
3
  • 4
    This was the problem for me. I replaced the map function with a for loop, which was an easy solution for me. However, this solution might not work for you depending on your code.
    – Thomas
    Commented Mar 13, 2019 at 20:26
  • 7
    FYI you can also do someArray.map(async (someVariable) => { return await someFunction(someVariable)})
    – ptim
    Commented Apr 10, 2019 at 13:02
  • 1
    The await in your code is misleading, because Array.map will not handle the function as an asynchronous function. To be perfectly clear, after the map function is finished, the someFunction will all be pending. If you want to really wait the functions to finish you have to write : await Promise.all(someArray.map(someVariable => someFunction(someVariable))) or await Promise.all(someArray.map(someFunction))).
    – Orelsanpls
    Commented Jan 8, 2020 at 17:04
17

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);
});
2
  • Thanks a lot! I read this answer after two hours XD Commented Feb 24, 2021 at 18:35
  • Do note that async and .forEach() usually don't work very well together. For instance, the code after the .forEach() will run before the code after the first await inside of your loop. And there is also no guarantee that (in this example) displayCommit() is invoked in the same order as the elements are looped over. See Using async/await with a forEach loop
    – Ivar
    Commented Nov 26, 2022 at 17:42
15

If you are writing a Chrome Extension and you get this error for your code at root, you can fix it using the following "workaround":

async function run() {
    // Your async code here
    const beers = await fetch("https://api.punkapi.com/v2/beers");
}

run();

Basically you have to wrap your async code in an async function and then call the function without awaiting it.

1
  • Would you know why this happens in chrome? Commented Mar 17, 2021 at 19:43
13

In later nodejs (>=14), top await is allowed with { "type": "module" } specified in package.json or with file extension .mjs.

https://www.stefanjudis.com/today-i-learned/top-level-await-is-available-in-node-js-modules/

4

async/await is the mechanism of handling promise, two ways we can do it

functionWhichReturnsPromise()
            .then(result => {
                console.log(result);
            })
            .cathc(err => {
                console.log(result);

            });

or we can use await to wait for the promise to full-filed it first, which means either it is rejected or resolved.

Now if we want to use await (waiting for a promise to fulfil) inside a function, it's mandatory that the container function must be an async function because we are waiting for a promise to fulfiled asynchronously || make sense right?.

async function getRecipesAw(){
            const IDs = await getIds; // returns promise
            const recipe = await getRecipe(IDs[2]); // returns promise
            return recipe; // returning a promise
        }

        getRecipesAw().then(result=>{
            console.log(result);
        }).catch(error=>{
            console.log(error);
        });
1
  • Yes, it does. And in order to call the async function, you will need to use await in the caller, and because you need await there, that function mist also be async. Basically, if you want to use async/await at all, you are going to have to use it on ALL of your functions across your entire app. Commented Sep 12, 2020 at 16:43
3

The current implementation of async / await only supports the await keyword inside of async functions Change your start function signature so you can use await inside start.

 var start = async function(a, b) {

 }

For those interested, the proposal for top-level await is currently in Stage 2: https://github.com/tc39/proposal-top-level-await

3
  • 3
    Unfortunately, what this basically means is that you are going to have to make ALL of your functions async, across your entire code base. Because if you want to use await, you must do it in an async function, which means that you must await the response of that function in the function calling it - again, it means that ALL of your functions will need to become async. To me this means that await async is not ready for use. When you can use await to call an async method, regardless of whether the current function is synchronous or asynchronous, then it will be ready for prime time. Commented Jan 7, 2020 at 19:16
  • 1
    Every function which is through any level of indirection dependent on the results of an external process must, and ought to be defined with async - that's the entire point of async. Commented Jan 7, 2020 at 20:16
  • You can currently use it in node repl using --experimental-repl-await option.
    – Lodz
    Commented May 21, 2020 at 12:00
2

If you have called async function inside foreach update it to for loop

2

Found the code below in this nice article: HTTP requests in Node using Axios

const axios = require('axios')

const getBreeds = async () => {
  try {
    return await axios.get('https://dog.ceo/api/breeds/list/all')
  } catch (error) {
    console.error(error)
  }
}

const countBreeds = async () => {
  const breeds = await getBreeds()

  if (breeds.data.message) {
    console.log(`Got ${Object.entries(breeds.data.message).length} breeds`)
  }
}

countBreeds()

Or using Promise:

const axios = require('axios')

const getBreeds = () => {
  try {
    return axios.get('https://dog.ceo/api/breeds/list/all')
  } catch (error) {
    console.error(error)
  }
}

const countBreeds = async () => {
  const breeds = getBreeds()
    .then(response => {
      if (response.data.message) {
        console.log(
          `Got ${Object.entries(response.data.message).length} breeds`
        )
      }
    })
    .catch(error => {
      console.log(error)
    })
}

countBreeds()
1

A common problem in Express:

The warning can refer to the function, or where you call it.

Express items tend to look like this:

app.post('/foo', ensureLoggedIn("/join"), (req, res) => {
    const facts = await db.lookup(something)
    res.redirect('/')
})

Notice the => arrow function syntax for the function.

The problem is NOT actually in the db.lookup call, but right here in the Express item.

Needs to be:

app.post('/foo', ensureLoggedIn("/join"), async function (req, res) {
    const facts = await db.lookup(something)
    res.redirect('/')
})

Basically, nix the => and add async function .

0

This in one file works..

Looks like await only is applied to the local function which has to be async..

I also am struggling now with a more complex structure and in between different files. That's why I made this small test code.

edit: i forgot to say that I'm working with node.js.. sry. I don't have a clear question. Just thought it could be helpful with the discussion..

    function helper(callback){



    function doA(){

        var array = ["a ","b ","c "];

        var alphabet = "";

        return new Promise(function (resolve, reject) {

            array.forEach(function(key,index){

            alphabet += key;

                if (index == array.length - 1){

                    resolve(alphabet);

                };

            });

        });

    };



    function doB(){

        var a = "well done!";

        return a;

    };



    async function make() {

        var alphabet = await doA();
        var appreciate = doB();

        callback(alphabet+appreciate);

    };

    make();

};

helper(function(message){

    console.log(message);

});
1
  • 1
    There is a problem with your question, namely.... it is not a question. You say you are struggling, so tell us why you are struggling and ask a to-the-point question...
    – Klaassiek
    Commented Apr 18, 2021 at 12:36
0

I encounter this problem when I'm using TypeScript. finally I figured out that I should set target: "ES2017" in tsconfig.json so that the transpiled javascript code keep the async function literals instead of __await ... code under ES6 behavior.

-3

Yes, await / async was a great concept, but the implementation is completely broken.

For whatever reason, the await keyword has been implemented such that it can only be used within an async method. This is in fact a bug, though you will not see it referred to as such anywhere but right here. The fix for this bug would be to implement the await keyword such that it can only be used TO CALL an async function, regardless of whether the calling function is itself synchronous or asynchronous.

Due to this bug, if you use await to call a real asynchronous function somewhere in your code, then ALL of your functions must be marked as async and ALL of your function calls must use await.

This essentially means that you must add the overhead of promises to all of the functions in your entire application, most of which are not and never will be asynchronous.

If you actually think about it, using await in a function should require the function containing the await keyword TO NOT BE ASYNC - this is because the await keyword is going to pause processing in the function where the await keyword is found. If processing in that function is paused, then it is definitely NOT asynchronous.

So, to the developers of javascript and ECMAScript - please fix the await/async implementation as follows...

  • await can only be used to CALL async functions.
  • await can appear in any kind of function, synchronous or asynchronous.
  • Change the error message from "await is only valid in async function" to "await can only be used to call async functions".
11
  • You can call it a bug if you like, but I disagree. There is no such thing as code which "pauses" - rather, there is code which cannot complete without the results of some external process (usually io). Such code should be called "asynchronous" as many external processes should be able to run at the same time (non-synchronously), in contrast to the javascript VM which is single-threaded. If you have many functions which need to be refactored to async that reflects the fact that many of your functions require the results of external processes. That is completely canonical in my opinion. Commented Jan 7, 2020 at 20:09
  • It's also worth mentioning a terrible drawback of restricting await to only be usable with function calls: for a single external process, only a single point in javascript code could be notified when that process completes. For example if the content of a file is needed for 3 independent purposes each purpose would need to independently do let content = await readTheFile(); - this is because the "promise of the file's content" cannot be awaited, only "the act of reading the file and resuming once it's been read". Commented Jan 7, 2020 at 20:14
  • Ok, let's not call it code that pauses, or code which cannot complete, but how about blocked waiting. Here's the rub - the function that is blocked waiting or that cannot complete is the function that contains the await keyword. It is not the async function that is being called with the await keyword. Hence, the function containing the await keyword should definitely NOT have to be marked as async - it is blocked waiting, which is the opposite of asynchronous. Commented Jan 7, 2020 at 20:30
  • To make this utterly clear, consider the following - await is intended to simplify the use of asynchronous functions by making them appear to be synchronous (i.e. it allows me to do things in a specific order). Forcing the function containing the await to be async is a complete misnomer - you used await so that it becomes synchronous. A function containing an await is absolutely, in every conceivable manner, NOT an async function!!! Commented Jan 7, 2020 at 20:40
  • 1
    @Gershom - that sounds reasonable. Thanks! Commented Feb 21, 2020 at 19:59
-3

"await is only valid in async function"

But why? 'await' explicitly turns an async call into a synchronous call, and therefore the caller cannot be async (or asyncable) - at least, not because of the call being made at 'await'.

2
  • 1
    Actually, await does not wait for results - it immediately returns a promise. This is exactly what I was attempting to convey. If await actually waited and did not return control to the caller, then any function that contained an await keyword would literally not be able to be marked async. But instead of that, we have any function that contains await or calls a function that eventually calls a function containing await must be async. Basically, if you call await even one time - all of your functions must be marked async. Commented Feb 20, 2020 at 20:08
  • Nope. It does not 'turn an async call into a synchronous call'. Async/await is still asynchronous - it just looks synchronous. That's my big problem with it, tbh...it's "too clever" and pretends to be something it is not, just inviting users to trip up. Checkout generator functions and 'yield' to get a glimpse of what is really happening, though I find that to be even more of a headache. Commented Mar 16, 2021 at 6:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.