Skip to main content

Overview

Handles the Err case of a ResultAsync by applying a recovery function. Useful for error recovery, fallbacks, and error transformation.

Signature

class ResultAsync<T, E> {
  orElse<U, A>(
    f: (e: E) => Result<U, A> | ResultAsync<U, A>
  ): ResultAsync<U | T, A>
}
f
(e: E) => Result<U, A> | ResultAsync<U, A>
required
Function that takes the Err value and returns a new Result or ResultAsync
Returns: ResultAsync<U | T, A> - A ResultAsync with potentially recovered value

Usage

Simple fallback

const result = await fetchFromCache(key)
  .orElse(() => fetchFromDatabase(key))
  .orElse(() => okAsync(defaultValue))

// Tries cache, then database, then default

Conditional recovery

type ApiError = { code: number, message: string }

const result = await callApi()
  .orElse((error: ApiError) => {
    if (error.code === 404) {
      return okAsync(null) // Treat 404 as empty result
    }
    if (error.code === 503) {
      return retryAfterDelay() // Retry on service unavailable
    }
    return errAsync(error) // Propagate other errors
  })

Multi-tier fallback

const loadConfig = () => {
  return readFromFile('./config.json')
    .orElse(() => readFromFile('./config.default.json'))
    .orElse(() => fetchFromRemote())
    .orElse(() => okAsync(HARDCODED_DEFAULTS))
}

Key characteristics

  • Only runs on Err: If the ResultAsync is Ok, orElse is not executed
  • Can change type: The recovery function can return a different value type
  • Error type changes: The new error type replaces the original
Use orElse to implement graceful degradation in your async operations.

andThen

Chain successful operations

unwrapOr

Provide a simple default value

Build docs developers (and LLMs) love