liveBook · Manning

Closed context

As explained in The Joy of JavaScript

An execution context that encloses and controls the behavior of operations without exposing internal control flow to the outside.

A closed context encloses computation so its internal control flow and effects remain behind an abstraction boundary. Operations interact with the context through defined transformations rather than managing every failure or state transition themselves. section 5.0 section 8.2.2

Validation uses this boundary to contain checking logic. Instead of inserting validation before every composed function or repeating try/catch blocks, a chain carries successful computation forward while recording failure and stopping subsequent operations. section 5.0

Promises apply the same idea to time. A Promise begins pending or fulfilled, changes according to executor or reaction outcomes, and lets then compose work without exposing its asynchronous control flow. Rejected computation can fall through the chain to catch. section 8.2.2

The pattern matters because it:

A validation pipeline

Validation.of(block) .flatMap(checkLength(64)) .flatMap(checkTampering) .flatMap(checkDifficulty) .flatMap(checkLinkage(previousHash)) .flatMap(checkTimestamps(previousTimestamp)); Each check operates inside the Validation context. The pipeline expresses the order of checks while the context manages whether later computations continue. section 5.0

const p = new Promise(resolve => {
  setTimeout(() => {
    resolve('Done');
  }, 3000);  
});

console.log(p); // Promise { <pending> }

A pending Promise makes the hidden time boundary visible: the object exists immediately while its computation completes later. section 8.2.2

How The Joy of JavaScript covers it

How deep it goes
Defines the abstraction, contrasts repeated checks and try/catch, and traces Promise states and composition without deriving implementation internals.
Examples
Demonstrates chained block validation, repeated try/catch alternatives, pending Promises, and Promise transformations.
What it assumes
Assumes familiarity with function composition and JavaScript callbacks while teaching closed contexts through Validation and Promise examples.
What you can do after
Apply it via Validation and Promise chains to compose operations while containing validation, failure, and time effects.
How it connects
Connects algebraic data types, Validation, Promise, functors, monads, composition, and error handling.

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