在飞镖语言中,异步/等待的确切而清晰的含义是什么

发布于 2025-02-12 12:18:18 字数 768 浏览 0 评论 0原文

我了解异步编程的概念使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扛起拖把扫天下 2025-02-19 12:18:18

我了解使用未来的飞镖中异步编程的概念。通过这样做,我们尝试在后台进行异步而不是停止应用程序。

但这并不是真正的“背景”。 DART是合作的单线螺纹(如果我们将隔离暂时排除在外)。一次只运行一个飞镖线。

因为等待这个词会导致该程序暂时停在那里,直到完成该操作,这完全违反了异步编程的概念。

不,或多或少意味着与之相反。这更像是“我们有一些不完整的东西,如果我们等待的话会阻止我们...让我们在这里放一个书签,继续前进,然后稍后再回来看看是否完成了……”

因此,与“普通代码不同, “这必须不屈服,等待“去另一个绿色线程”的机会。

调度程序可以在此线程中暂停某些操作中的执行,例如致电等待,返回未来,呼叫收益,未来等等。

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 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.

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文