Coroutine vs Continuation vs Generator

I’ll start with generators, seeing as they’re the simplest case. As @zvolkov mentioned, they’re functions/objects that can be repeatedly called without returning, but when called will return (yield) a value and then suspend their execution. When they’re called again, they will start up from where they last suspended execution and do their thing again. A … Read more

What’s the difference between a continuation and a callback?

I believe that continuations are a special case of callbacks. A function may callback any number of functions, any number of times. For example: var array = [1, 2, 3]; forEach(array, function (element, array, index) { array[index] = 2 * element; }); console.log(array); function forEach(array, callback) { var length = array.length; for (var i = … Read more

Implementing yield (yield return) using Scala continuations

Before we introduce continuations we need to build some infrastructure. Below is a trampoline that operates on Iteration objects. An iteration is a computation that can either Yield a new value or it can be Done. sealed trait Iteration[+R] case class Yield[+R](result: R, next: () => Iteration[R]) extends Iteration[R] case object Done extends Iteration[Nothing] def … Read more

Async/Await – is it *concurrent*?

It is concurrent, in the sense that many outstanding asychronous operations may be in progress at any time. It may or may not be multithreaded. By default, await will schedule the continuation back to the “current execution context”. The “current execution context” is defined as SynchronizationContext.Current if it is non-null, or TaskScheduler.Current if there’s no … Read more