Railway-Oriented Programming
Chain operations that may fail without try/catch.
The Railway Metaphor
Railway-oriented programming pictures a computation as two parallel tracks: a success track and a failure track. Once you switch to failure, you stay there, skipping the rest.
Chaining Result Functions
Each step takes a value and returns a Result. We want to connect steps so that the first failure short-circuits the chain and is carried to the end.
type Result<T, E> = { ok: true; value: T } | { ok: false; error: E };
const ok = <T>(value: T): Result<T, never> => ({ ok: true, value });
const err = <E>(error: E): Result<never, E> => ({ ok: false, error });All lessons in this course
- The Problem with Throwing Errors
- Modeling Result Types
- Option and Maybe Types
- Railway-Oriented Programming