为什么'await'在函数定义中需要'async' [英] Why 'await' requires 'async' in function definition
问题描述
我目前正在学习Dart,但这也适用于JavaScript世界中目前正在发生的事情,而且C#似乎也使用相同的模式.
I'm currently learning Dart, but this is also applicable to what's going on in the JavaScript world right now, and it seems like C# also uses the same pattern.
在Dart中,任何使用 await
的函数本身都必须通过 async
标记为异步,如下所示:
In Dart, any function that uses await
must itself be labeled asynchronous through async
as follows:
import "dart:html";
main() async {
var context = querySelector("canvas").context2D;
var running = true;
while (running) {
var time = await window.animationFrame;
...
}
}
这对我来说没有意义.如果某个函数正在等待异步函数完成,那么是否不认为它阻塞了?为什么JS和Dart要求将其标记为异步?是不是相反?
This does not make sense to me. If a function is waiting on an asynchronous function to complete, is it not then considered blocking? Why do JS and Dart require it to be labeled asynchronous? Would it not be the opposite?
对我而言,如果调用函数调用在其定义中也包含该函数的任何函数,则必须使用 async
关键字会更有意义.在这种模式下, await
将用于将异步函数转换为同步函数.
To me it would make far more sense if the calling function must use the async
keyword if it calls any function that also includes it in its definition. In this pattern, await
would be used to convert asynchronous functions to synchronous ones.
这种方式还可以避免重复功能,因为现在,库似乎总是具有 func()
和 funcSync()
或 funcAsync()代码>.
This way would also avoid duplicate functions, since right now, libraries seem to always have func()
and funcSync()
or funcAsync()
.
推荐答案
async
/ await
的基本语义在F#,VB,C#,Python,Dart,Hack和JavaScript.因此,我认为这个问题可以从其他语言中获得足够的答案.但是由于它已经重新打开...
The basic semantics of async
/await
are the same across F#, VB, C#, Python, Dart, Hack, and JavaScript. So I think this question has sufficient answers from other languages. But since it has been reopened...
如果某个函数正在等待异步函数完成,那么是否不认为它阻塞了?
If a function is waiting on an asynchronous function to complete, is it not then considered blocking?
不.这样想吧:
- 异步"的意思是不阻塞调用线程".
- 同步"是指阻止调用线程".
在异步方法/函数中,可以在 await
点处暂停该方法/函数,但是在暂停时它不会阻塞调用线程.该函数运行 serially (一次运行一个语句),但异步(不阻塞调用线程).
In an asynchronous method/function, the method/function can be paused at the await
points, but it does not block the calling thread while it is paused. The function runs serially (one statement at a time), but asynchronously (without blocking the calling thread).
对我来说,如果调用函数调用在其定义中也包含该函数的任何函数,则必须使用async关键字会更有意义.
To me it would make far more sense if the calling function must use the async keyword if it calls any function that also includes it in its definition.
这就是它的工作原理... await
消耗了promise/future/task-returning方法/函数,而 async
将方法/函数标记为可以使用等待
.
That's how it already works... await
consumes promise/future/task-returning methods/functions, and async
marks a method/function as capable of using await
.
这样也可以避免重复功能
This way would also avoid duplicate functions
这在历史上受阻的命令性语言中是不可能的.方法/函数要么阻塞调用线程,直到完成,要么不阻塞.它可以是同步的或异步的.
This is not possible with historically-blocking imperative languages. A method/function either blocks the calling thread until it is complete, or it does not. It is either synchronous or asynchronous.
不过,异步方法/函数有一些有趣的替代方法:
There are some interesting alternative approaches to async methods/functions, though:
从历史上看,Go并没有阻塞;您可以将其视为一种语言,其中每种方法/函数都可能是异步的;在任何其他运行时中执行此操作都将是灾难性的,但是Go通过实施具有消息传递功能的严格goroutine系统来避免这些问题-不允许共享内存或线程.
Go is not historically blocking; you can think of it as a language where every method/function is potentially asynchronous; doing this in any other runtime would be disastrous, but Go avoids those problems by implementing a strict goroutine system with message passing - no shared memory or threads allowed.
另一个示例是纯函数式语言,其中Future只是另一种语言,不需要 async
/ await
关键字的特殊编译器支持.
Another example are pure functional languages, where Future is just another monad and requires no special compiler support for async
/await
keywords.
这篇关于为什么'await'在函数定义中需要'async'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!