Skip to main content

Overview

ResultAsync<T, E> is a class that wraps a Promise<Result<T, E>>, providing a seamless way to work with asynchronous operations that may fail.
Source: result-async.ts:22-27 The key insight: ResultAsync is essentially a Promise<Result<T, E>> with additional methods that let you work with the Result without having to await it first.

Why ResultAsync?

The Problem with Async Results

When working with promises and results together, you’d normally have to write:

The Solution

ResultAsync lets you chain operations without awaiting:

Creating ResultAsync

Method 1: okAsync and errAsync

Create a ResultAsync from a value:
Source: result-async.ts:247-256

Method 2: fromPromise

Wrap an existing promise that may reject:
Source: result-async.ts:36-42
The second argument to fromPromise is an error handler that converts the unknown error into your error type E.

Method 3: fromSafePromise

Wrap a promise that you know will never reject:
Source: result-async.ts:29-34
Only use fromSafePromise when you’re absolutely certain the promise won’t reject. If it does reject, the ResultAsync will reject the promise instead of resolving to an Err.

Method 4: fromThrowable

Wrap a function that returns a promise and may throw:
Source: result-async.ts:45-61
fromThrowable is safer than fromPromise because it catches both synchronous throws (before the promise is returned) and asynchronous rejections.

The Thenable Behavior

ResultAsync implements PromiseLike<Result<T, E>>, which means it’s “thenable” and can be used with await and .then():
Source: result-async.ts:226-232

Using with async/await

Using with .then()

Mixing with Promise.all

Methods: Sync vs Async Parameters

Many ResultAsync methods accept both synchronous and asynchronous functions:

map - Synchronous or Asynchronous

Source: result-async.ts:89-99

mapErr - Transform Errors

Source: result-async.ts:149-159

andThen - Chain Dependent Operations

Source: result-async.ts:161-180
andThen accepts both Result and ResultAsync return types, automatically handling the conversion.

Chaining Operations

One of the most powerful features of ResultAsync is the ability to chain multiple asynchronous operations:

Side Effects with andTee and orTee

andTee - Side Effects on Success

Execute a side effect on success without changing the value:
Source: result-async.ts:117-131

orTee - Side Effects on Error

Execute a side effect on error without changing the error:
Source: result-async.ts:133-147
Use andTee and orTee for logging, metrics, or other side effects that shouldn’t affect your main logic flow.

Advanced Patterns

Pattern 1: Parallel Operations with combine

Source: result-async.ts:63-73

Pattern 2: Collecting All Errors

Source: result-async.ts:75-87

Pattern 3: Sequential with Error Recovery

Pattern 4: Transform and Unwrap

Comparison: Promise of Result vs ResultAsync

Visual Flow

Performance Considerations

Chaining is Efficient

Avoid Unnecessary Awaits

Type Signatures from Source

Next Steps

Result Type

Learn about the synchronous Result type

Error Handling Philosophy

Understand why encoding errors in types is better than throwing