Skip to main content

Overview

Extracts the Ok value from a ResultAsync, or returns the provided default value if it’s an Err. Returns a Promise of the value.

Signature

class ResultAsync<T, E> {
  unwrapOr<A>(t: A): Promise<T | A>
}
t
A
required
The default value to return if the ResultAsync is an Err
Returns: Promise<T | A> - A Promise resolving to the Ok value or the default

Usage

Basic usage

const value = await errAsync(0).unwrapOr(10)
// value is 10

const value2 = await okAsync(5).unwrapOr(10)
// value2 is 5

Configuration with defaults

const config = await loadConfig()
  .unwrapOr({
    host: 'localhost',
    port: 3000,
    debug: false
  })

// config is always defined, uses defaults if loading fails

API responses

async function getUserName(id: number): Promise<string> {
  return fetchUser(id)
    .map(user => user.name)
    .unwrapOr('Anonymous')
}

const name = await getUserName(123)
// name is string, never Result

Chaining with transformations

const result = await fetchData()
  .map(data => data.value)
  .map(value => value * 2)
  .unwrapOr(0)

// result is number

Key characteristics

Use unwrapOr at the end of a Result chain when you want to exit the Result context and always get a value.
  • Always succeeds: Never throws, always returns a value
  • Type safety: Return type is union of Ok and default types
  • Returns Promise: Requires await or .then() to access the value

When to use

  • At application boundaries where you need concrete values
  • When a sensible default exists for error cases
  • Converting Result-based code to traditional async code
The default value is evaluated eagerly. If computing it is expensive, consider using match() or orElse() instead.

match

Handle both cases with callbacks

orElse

Compute default based on the error

Build docs developers (and LLMs) love