Getting Started
Install @zap-studio/monads and handle your first fallible operation with Result.
This page walks you through installing @zap-studio/monads and using Result for a value that might fail. For deeper details, check the other pages in this section.
Install
npm install @zap-studio/monadsyarn add @zap-studio/monadspnpm add @zap-studio/monadsbun add @zap-studio/monadsdeno add jsr:@zap-studio/monadsWrite a Function That Returns a Result
Instead of throwing, return ok(value) on success and err(reason) on failure:
import { err, ok, Result } from "@zap-studio/monads";
function parseAge(input: string): Result<number, string> {
const value = Number(input);
return Number.isNaN(value) ? err("not a number") : ok(value);
}
Compose With pipe
Result’s combinators are curried functions, not methods — compose them with pipe:
import { pipe, Result } from "@zap-studio/monads";
const message = pipe(
parseAge("42"),
Result.map((age) => age + 1),
Result.match({
ok: (age) => `Age next year: ${age}`,
err: (reason) => `Invalid input: ${reason}`,
}),
);
Extract the Value
Use Result.unwrapOr for a default, Result.unwrapOrElse to compute a fallback from the error, or Result.unwrap to throw on Err:
import { pipe, Result } from "@zap-studio/monads";
pipe(parseAge("not a number"), Result.unwrapOr(0)); // 0