What's the relationship between the async/await pattern and continuations?
I'm wondering what's the relationship between the async
/ await
pattern (as known from Scala, F#, C#, etc.) and continuations:
async
/ await
pattern a limited subset of full-blown continuations? (If true, how are continuations more expressive?) async
/ await
? (If true, what other implementation approaches exist?) async
/ await
and continuations just orthogonal concepts where the only commonality is that they both enable some abstraction of control flow/data flow? I would say that the relation between the two is this: async
- await
is a technique programming languages use so that you can write code that looks synchronous (eg no explicit continuation delegates), but that is actually executed asynchronously. This is achieved by creating an object that represents the current state of execution of the function and registering that as the continuation of the awaited operation.
In short, async
- await
uses continuations.
Is the async
/ await
pattern a limited subset of full-blown continuations? (If true, how are continuations more expressive?)
You could say that. Continuations are a more general concept, async
- await
just uses them to achieve asynchrony.
For example, in continuation-passing style programming, you could implement exception handling by having two continuations for each operation: one for the success case and one for the failure case. This use of continuations has nothing to do with async
- await
(you would have to write each continuation explicitly, probably as a lambda).
Are continuations just one possible implementation technique for async
/ await
? (If true, what other implementation approaches exist?)
I'd say that the concept of continuation is pretty central to async
- await
.
The core idea being async
- await
is to stop executing the function for now and resume it at a later time. And for that, you need some kind of object that can be used to do that resuming. Which is exactly what a continuation is.
上一篇: 回归表中的参考类别
下一篇: 异步/等待模式和延续之间有什么关系?