Skip to main content

Overview

The Result type has four primary ways to create instances:
  • Result.ok() for successful values
  • Result.err() for error values
  • Result.try() for wrapping sync functions that may throw
  • Result.tryPromise() for wrapping async functions with retry support

Creating Success Results

Result.ok()

Creates an Ok instance wrapping a successful value.

Creating Ok<void>

For side-effectful operations that don’t return a meaningful value, call Result.ok() without arguments:
Result.ok() creates an Ok<void, never> which is compatible with Result<void, E> for any error type E.

Type Signature

Creating Error Results

Result.err()

Creates an Err instance wrapping an error value.

Using TaggedError for Discriminated Errors

For type-safe error handling, use TaggedError to create discriminated error classes:
See Error Handling for comprehensive TaggedError patterns.

Type Signature

Wrapping Throwing Functions

Result.try()

Executes a synchronous function and wraps the result or exception in a Result.

Basic Usage

Custom Error Handling

Transform caught exceptions into domain-specific errors using the catch handler:
If your catch handler throws, Result.try will throw a Panic. Catch handlers should always return an error value, never throw.

Retry on Failure

Type Signature

Wrapping Async Functions

Result.tryPromise()

Executes an async function and wraps the result or rejection in a Result, with advanced retry support.

Basic Usage

Custom Error Handling

Retry with Exponential Backoff

Conditional Retry

Use shouldRetry to retry only specific errors:
The shouldRetry predicate receives the error returned by your catch handler, allowing you to make retry decisions based on enriched error context.

Backoff Strategies

Type Signature

When to Use Each Method

Use when you already have a success value and want to wrap it in a Result.

Next Steps

Transforming Results

Learn how to transform success and error values with map, mapError, and andThen

Error Handling

Master TaggedError, exhaustive matching, and error recovery patterns