FP #3 — Maybe & Either Monads

Build Maybe (Some/None) and Either (Right/Left) monads for error-safe pipelines. Chain operations without null checks or try/catch litter.

12 min read
JavaScript
Functional
Monad
Error Handling

TABLE OF CONTENTS
FP #3 — Maybe & Either Monads

A monad wraps a value and lets you chain operations without worrying about null, undefined, or errors. The Maybe monad handles nullable values. The Either monad handles success/failure paths. Both eliminate defensive null checks and try/catch litter.

Prerequisites: FP #1 — compose/pipe


1. The Problem — Defensive Null Checks Everywhere

Loading editor...


2. Maybe Monad — Chain Without Null Checks

Maybe wraps a value that might be null/undefined. Operations are applied only if the value exists:

Loading editor...


3. Either Monad — Success/Failure Paths

Either has two variants: Right (success) and Left (failure). map only runs on Right. chain enables branching:

Loading editor...


4. Railway-Oriented Programming Visualized

Loading editor...


5. Chaining Async Operations with Either

Loading editor...


Key Takeaways

  • Maybe: wraps nullable values — map/chain skip when null/undefined. Use for deep property access.
  • Either/Result: wraps operations that may fail — Right for success, Left for failure.
  • .map(fn): transforms the wrapped value (if it exists/is success).
  • .chain(fn): like map but fn returns a new monad — enables branching.
  • .fold(errFn, okFn): unwrap both paths at the end — the only place errors are handled.
  • Monads compose — chain Maybe with Either for null-safe, error-safe pipelines.

Next: FP #4 — Point-Free Style & Referential Transparency — write functions without naming arguments.


Let's Connect

© 2026 Naveen Karthik // Built with React & MUI