Overview
The Result type has four primary ways to create instances:Result.ok()for successful valuesResult.err()for error valuesResult.try()for wrapping sync functions that may throwResult.tryPromise()for wrapping async functions with retry support
Creating Success Results
Result.ok()
Creates anOk instance wrapping a successful value.
Creating Ok<void>
For side-effectful operations that don’t return a meaningful value, callResult.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 anErr instance wrapping an error value.
Using TaggedError for Discriminated Errors
For type-safe error handling, useTaggedError to create discriminated error classes:
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 thecatch handler:
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
UseshouldRetry 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
- Result.ok()
- Result.err()
- Result.try()
- Result.tryPromise()
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