9

I'm wondering what's the relationship between the async/await pattern (as known from Scala, F#, C#, etc.) and continuations:

  • Is the async/await pattern a limited subset of full-blown continuations? (If true, how are continuations more expressive?)
  • Are continuations just one possible implementation technique for async/await? (If true, what other implementation approaches exist?)
  • Or are async/await and continuations just orthogonal concepts where the only commonality is that they both enable some abstraction of control flow/data flow?
4

1 回答 1

5

我想说两者之间的关系是这样的:async-await是一种编程语言使用的技术,因此您可以编写看起来同步的代码(例如,没有显式的延续委托),但实际上是异步执行的。这是通过创建一个表示函数当前执行状态的对象并将其注册为等待操作的继续来实现的。

简而言之,async-await 使用延续。

async/模式是await成熟延续的有限子集吗?(如果为真,延续如何更具表现力?)

你可以那样说。延续是一个更一般的概念,async-await只是使用它们来实现异步。

例如,在延续传递风格的编程中,您可以通过为每个操作设置两个延续来实现异常处理:一个用于成功案例,一个用于失败案例。async延续的这种使用与-无关await(您必须显式地编写每个延续,可能作为 lambda)。

async延续只是/的一种可能的实现技术await吗?(如果属实,还有哪些其他实现方法?)

我会说 continuation 的概念对于async-非常重要await

核心思想是async-暂时await停止执行该功能并在以后恢复它。为此,您需要某种可用于恢复的对象。这正是延续。

于 2014-04-03T11:58:20.027 回答