Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • 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?
like image 599
soc Avatar asked Apr 02 '14 16:04

soc


1 Answers

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 (e.g. 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.

like image 142
svick Avatar answered Sep 26 '22 03:09

svick