Overview
Maps aResult<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 aResultAsync<U, E> which can be awaited or chained with more ResultAsync methods.
- If the original Result is
Ok(value), the returned ResultAsync resolves toOk(await f(value)) - If the original Result is
Err(error), the returned ResultAsync resolves toErr(error)without callingf
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):Err (result.ts:484-486):
Notes
- Always returns a
ResultAsync, even when starting with a synchronousResult - The async function is only executed for
Okvalues - Errors in the original Result are propagated without awaiting anything
- The async function must return a
Promise, not aResultorResultAsync - For functions returning
ResultAsync, use asyncAndThen() instead - Exceptions thrown by the async function will cause the promise to reject (not convert to Err)
Related
- Result.map() - Synchronous transformation
- Result.asyncAndThen() - Chain with functions returning ResultAsync
- ResultAsync - Full async Result API