By example: Continuation-passing style in JavaScript

Continuation-passing style (CPS) originated as a style of programming in
the 1970s, and it rose to prominence as an intermediate representation for
compilers of advanced programming languages in the 1980s and 1990s.

It's now being rediscovered as a style of programming for
non-blocking (usually distributed) systems.

There's a warm spot in my heart for CPS, because it was the secret weapon
in my Ph.D.
It probably shaved off a couple years and immeasurable agony.

This article introduces CPS in both of its roles--as a style for
non-blocking programming in JavaScript, and (briefly) as an intermediate form
for a functional language.

Topics covered:

CPS in JavaScript

CPS for Ajax programming

CPS for non-blocking programming (in node.js)

CPS for distributed programming

How to implement exceptions using CPS

A CPS converter for a minimal Lisp

How to implement call/cc in Lisp

How to implement call/cc in JavaScript

Read on for more.

What is continuation-passing style?

If a language supports continuations, the programmer can add control
constructs like exceptions, backtracking, threads and generators.

Sadly, many explanations of continuations
(mine
included) feel vague and unsatisfying.
Such power deserves a solid pedagogical foundation.