我在lib/helper.js中编写了以下代码:

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

然后我尝试在另一个文件中使用它:

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

我得到一个错误:

await is only valid in async function

问题是什么?


当前回答

Async /await是处理promise的机制,有两种方式

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

            });

或者我们可以使用await来等待promise先将它完全归档,这意味着它要么被拒绝,要么被解决。

现在,如果我们想在函数中使用await(等待一个承诺来实现),容器函数必须是一个异步函数,因为我们正在等待一个承诺来异步实现||,这是有意义的对吗?

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

其他回答

async / await的当前实现只支持async函数内部的await关键字。

 var start = async function(a, b) {

 }

对于那些感兴趣的人,顶层等待的提议目前处于阶段2:https://github.com/tc39/proposal-top-level-await

Async /await是处理promise的机制,有两种方式

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

            });

或者我们可以使用await来等待promise先将它完全归档,这意味着它要么被拒绝,要么被解决。

现在,如果我们想在函数中使用await(等待一个承诺来实现),容器函数必须是一个异步函数,因为我们正在等待一个承诺来异步实现||,这是有意义的对吗?

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

当我得到这个错误时,原来我在我的“async”函数中调用了map函数,所以这个错误消息实际上是指映射函数没有被标记为“async”。我绕过了这个问题,把“await”调用从map函数中取出,并提出了一些获得预期行为的其他方法。

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

“await只在异步函数中有效”

但是为什么呢?'await'显式地将异步调用转换为同步调用,因此调用方不能是异步的(或异步的)——至少不是因为调用是在'await'进行的。

这在一个文件工作..

看起来await only应用于本地函数,必须是异步的。

我现在也在与更复杂的结构作斗争,在不同的文件之间。这就是我编写这个小测试代码的原因。

编辑:我忘了说我正在使用node.js…sry基因。我没有一个明确的问题。只是觉得这对讨论有帮助。

    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);

});