This is an example showing how Scala continuations can be useful to implement a system with asynchronous events and/or I/O, very much like the current proposal for JavaScript deferred functions.

Here we simulate a single-threaded "system" (à la Node.js) which runs your application code and provides a non-blocking API. Here, the API is just one function, read(), which reads an int from some external source.

The application code is supposed to run quickly and to not block at all. The sooner it terminates, the better. Here what it means is that the code with reset (and after reset) should run quickly and then return to the system.

The example application code here reads two values in sequence using the read() function.

If we write it without continuations, the programming style is not very natural: even with Scala's pretty lightweight syntax for closures, you will typically nest callbacks within each other, and add some amount of boilerplate.

Introduce continuations with reset and shift, and voilà: the code now looks imperative (as in "step by step") again, while in fact, behind the scene, it never blocks on read() and still uses callbacks.