我在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

问题是什么?


错误不是指向myfunction,而是指向start。

async function start() {
   ....

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

//函数 Const myfunction = async函数(x, y) { 返回( x, y, ]; } //启动函数 Const start = async函数(a, b) { Const result = await myfunction('test', 'test'); console.log(结果); } //调用开始 开始();



我利用这个问题的机会来建议您使用await的一个已知的反模式,即:返回await。


错误的

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 aswell // 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'); })();


正确的

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 aswell // 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'); })();


另外,要知道有一种特殊情况,return await是正确且重要的:(使用try/catch)

“返回等待”是否存在性能问题?


要使用await,其执行上下文本质上需要是异步的

如上所述,您需要定义执行上下文的性质,在此上下文中,您愿意在任何事情之前等待一个任务。

只要把async放在你的异步任务将要执行的fn声明之前。

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

解释:

在您的问题中,您正在导入一个本质上是异步的并将并行执行的方法。但是当你试图执行异步方法时,你需要在一个不同的执行上下文中定义async来使用await。

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

想知道引擎盖下面是什么

Await使用promise/future / task-return方法/函数,async将一个方法/函数标记为能够使用Await。

另外,如果你熟悉承诺,等待实际上是在做同样的承诺/解决过程。创建承诺链,并在resolve回调中执行您的下一个任务。

更多信息请参考MDN DOCS。


当我得到这个错误时,原来我在我的“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);
    });
}

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

 var start = async function(a, b) {

 }

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


是的,await / async是一个很棒的概念,但是它的实现完全被破坏了。

不管出于什么原因,await关键字的实现使得它只能在异步方法中使用。这实际上是一个bug,尽管除了这里,您不会在任何地方看到它被称为bug。修复此错误的方法是实现await关键字,使其只能用于调用异步函数,而不管调用函数本身是同步的还是异步的。

由于这个错误,如果你在代码的某个地方使用await来调用一个真正的异步函数,那么你所有的函数都必须被标记为async,并且你所有的函数调用都必须使用await。

这本质上意味着您必须将promise的开销添加到整个应用程序中的所有函数中,其中大多数函数不是异步的,也永远不会是异步的。

如果你仔细想想,在函数中使用await应该要求包含await关键字的函数NOT BE ASYNC——这是因为await关键字将暂停找到await关键字的函数中的处理。如果该函数的处理被暂停,那么它肯定不是异步的。

所以,对于javascript和ECMAScript的开发者,请按以下方式修复await/async实现…

await只能用于调用异步函数。 Await可以出现在任何类型的函数中,同步或异步。 将错误信息从“await仅在async函数中有效”更改为“await只能用于调用async函数”。


我也遇到了同样的问题,下面的代码块给出了同样的错误信息:

repositories.forEach( repo => {
        const commits = await getCommits(repo);
        displayCommit(commits);
});

问题是方法getCommits()是异步的,但我传递给它的参数repo也是由承诺产生的。所以,我必须像这样添加单词async: async(repo),它开始工作:

repositories.forEach( async(repo) => {
        const commits = await getCommits(repo);
        displayCommit(commits);
});

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

但是为什么呢?'await'显式地将异步调用转换为同步调用,因此调用方不能是异步的(或异步的)——至少不是因为调用是在'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);
        });

如果你正在写一个Chrome扩展,你得到这个错误的代码在根,你可以修复它使用以下“变通”:

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

run();

基本上,你必须将异步代码包装在异步函数中,然后在不等待它的情况下调用该函数。


这在一个文件工作..

看起来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);

});

如果在foreach中调用了async函数,则将其更新为for循环


在这篇不错的文章中找到下面的代码:使用Axios的Node中的HTTP请求

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

或者用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()

Express的一个常见问题:

警告可以指向函数,也可以指向调用它的位置。

快递项目通常是这样的:

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

注意该函数的=>箭头函数语法。

问题并不在数据库中。查找调用,但就在Express项中。

需要:

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

基本上,删除=>并添加async函数。


在以后的nodejs(>=14)中,允许在package中指定{"type": "module"}来执行top await。Json或文件扩展名为.mjs。

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