Skip to content
Zap Studio
monads
Esc
navigateopen⌘Jpreview
On this page

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/monads
yarn add @zap-studio/monads
pnpm add @zap-studio/monads
bun add @zap-studio/monads
deno add jsr:@zap-studio/monads

Write 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

Last updated on September 21, 2026

Was this page helpful?