await is only valid in async function

后端 未结 9 1452
青春惊慌失措
青春惊慌失措 2020-11-22 06:53

I wrote this code in lib/helper.js

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


        
相关标签:
9条回答
  • 2020-11-22 07:30

    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".
    0 讨论(0)
  • 2020-11-22 07:35

    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);
            });
    
    0 讨论(0)
  • 2020-11-22 07:35

    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.

    0 讨论(0)
提交回复
热议问题