await 仅在 async 函数中有效

新手上路,请多包涵

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

问题是什么?

原文由 j.doe 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 964
2 个回答

该错误不是指 myfunction 而是指 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();


我利用这个问题的机会向您提供有关使用 await 的已知反模式的建议: return 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)

`return await` 是否存在性能问题?

原文由 Orelsanpls 发布,翻译遵循 CC BY-SA 4.0 许可协议

要使用 await ,其执行上下文本质上需要 async

正如它所说,您需要定义您的性质 executing context 您愿意 await 任何任务之前的任务。

只需将 async 放在 --- fn 声明之前,您的 async 任务将在其中执行。

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

解释:

在您的问题中,您正在导入 method asynchronous 并将并行执行。 But where you are trying to execute that async method is inside a different execution context which you need to define async to use await .

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

想知道引擎盖下发生了什么

await 承诺/未来/返回任务的方法/函数和 async 将方法/函数标记为能够使用等待。

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

有关更多信息,您可以参考 MDN 文档

原文由 Satyam Pathak 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题