Skip to main content

Overview

Takes an Err value and maps it to a Result<T, SomeNewType>. This is the error-handling counterpart to andThen, useful for error recovery and fallback logic.

Signature

Parameters

(e: E) => Result<U, A>
required
A function that takes the Err value and returns a new Result. This function is only called if the current Result is Err.

Returns

Returns a new Result<T | U, A> where:
  • If the original Result is Ok(value), returns Ok(value) without calling f
  • If the original Result is Err(error), returns the result of f(error)
  • The success type becomes a union of both possible success types

Examples

Basic Error Recovery

Error Recovery with Fallback

Multi-Level Fallbacks

Converting Errors to Success

Selective Error Recovery

Chaining Recovery Strategies

Real-World Example

Implementation Details

From the source code (result.ts:470-472):
For Ok (result.ts:366-368):

Notes

  • orElse operates on the error path, while andThen operates on the success path
  • Useful for implementing retry logic, fallbacks, and graceful degradation
  • The success type becomes a union of both possible success types
  • Can convert errors to successes for complete error recovery
  • For operations on Ok values, use andThen()