在飞镖语言中,异步/等待的确切而清晰的含义是什么
我了解异步编程的概念使用 Future
。 通过这样做,我们尝试在后台进行异步而不是停止应用程序。
但是,当使用async
/等待
时,我会感到困惑,因为它与异步操作的概念相矛盾! 由于等待
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 暂时停止那里,这完全违反了异步编程的概念。
另一方面,用飞镖语言 documentation ((我不明白这意味着什么):
注意:尽管异步功能可能会执行时间耗时 操作,它不会等待这些操作。相反,异步 函数仅执行直到遇到其第一个等待表达式 (细节)。然后它返回未来的对象,仅恢复执行 等待表达完成后。
通常,为什么要使用async
/等待异步编程(尽管它与其含义相矛盾)?
请清楚,简单地解释这些概念。 谢谢。
I understand the concept of asynchronous programming in Dart using Future
.
By doing this, we try to do things asynchronously in the background and not stop the application.
But I get confused when using async
/await
because it contradicts the concept of asynchronous operations! Because the word await
causes the program to be temporarily stopped there until that operation is finished, and this is completely against the concepts of asynchronous programming.
On the other hand, it is said in the Dart language documentation(And I don't understand what it means):
Note: Although an async function might perform time-consuming
operations, it doesn’t wait for those operations. Instead, the async
function executes only until it encounters its first await expression
(details). Then it returns a Future object, resuming execution only
after the await expression completes.
In general, why should we use async
/await
for asynchronous programming(Although it contradicts its meaning)?
Please explain these concepts clearly and simply.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
但这并不是真正的“背景”。 DART是合作的单线螺纹(如果我们将隔离暂时排除在外)。一次只运行一个飞镖线。
不,或多或少意味着与之相反。这更像是“我们有一些不完整的东西,如果我们等待的话会阻止我们...让我们在这里放一个书签,继续前进,然后稍后再回来看看是否完成了……”
因此,与“普通代码不同, “这必须不屈服,等待“去另一个绿色线程”的机会。
调度程序可以在此线程中暂停某些操作中的执行,例如致电等待,返回未来,呼叫收益,未来等等。
But it isn't really "in the background". Dart is cooperative single threaded (if we leave Isolates out of it for a moment). Only one dart thread is running at a time.
No, it more or less means the opposite of that. It's more like "we have something that is incomplete and would block us if we waited... let's put a bookmark here, go on to other things, and come back later to see if it's done..."
So unlike "ordinary code" that has to run without yielding, an await is an opportunity to "go to another green thread".
The scheduler can suspend execution in this thread on certain operations, like calling await, returning a Future, calling yield, Future.then, and so on.