liveBook · Manning

Continuation-passing style

As explained in Functional Programming in JavaScript

A programming style in which control is made explicit by passing the rest of the computation as a function argument.

Continuation-passing style (CPS) makes control explicit by passing the rest of a computation as a callback. It helps untangle asynchronous code by separating nested callbacks into named functions or lambda expressions, reducing unnecessary closures and making each step easier to follow. section 8.1

Refactoring asynchronous student retrieval

Instead of nesting callbacks while retrieving student information and grades, define separate handlers such as handleClickEvent, showStudent, processGrades, and handleError. Pass the next handler directly to getJSON, so each callback represents the current continuation. When asynchronous requests run inside a loop, pass the current student into a dedicated function to give each callback the correct closure.

How Functional Programming in JavaScript covers it

How deep it goes
Explains callback decomposition, continuations, closures, and context-stack efficiency, without developing a formal theory.
Examples
Uses XMLHttpRequest, nested student and grade requests, DOM events, and a loop that captures the wrong student.
What it assumes
Builds from nested callbacks and asynchronous control flow while exposing closures and function-stack behavior.
What you can do after
Apply it via JavaScript callbacks to separate asynchronous steps, reduce nesting, and preserve the correct data in closures.
How it connects
Connects asynchronous programming with callbacks, closures, functional programming, and the function stack.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Programming in JavaScript. The section links point to the parts of the book it draws on.