Skip to main content

Overview

Maps a Result<T, E> to ResultAsync<U, E> by applying an async function to a contained Ok value, leaving an Err value untouched. This bridges synchronous Results with asynchronous operations.

Signature

Parameters

(t: T) => Promise<U>
required
An async function that transforms the Ok value from type T to type U. This function is only called if the Result is Ok.

Returns

Returns a ResultAsync<U, E> which can be awaited or chained with more ResultAsync methods.
  • If the original Result is Ok(value), the returned ResultAsync resolves to Ok(await f(value))
  • If the original Result is Err(error), the returned ResultAsync resolves to Err(error) without calling f

Examples

Basic Usage

Database Query

Skips Errors

API Calls

Chaining Async Operations

File I/O

Parallel Async Operations

Real-World Authentication Flow

Error Propagation

Implementation Details

From the source code (result.ts:383-385):
For Err (result.ts:484-486):

Notes

  • Always returns a ResultAsync, even when starting with a synchronous Result
  • The async function is only executed for Ok values
  • Errors in the original Result are propagated without awaiting anything
  • The async function must return a Promise, not a Result or ResultAsync
  • For functions returning ResultAsync, use asyncAndThen() instead
  • Exceptions thrown by the async function will cause the promise to reject (not convert to Err)