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

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

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

 var start = async function(a, b) {

 }

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

在以后的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/

错误不是指向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)

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