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.
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:Method 2: fromPromise
Wrap an existing promise that may reject: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:Method 4: fromThrowable
Wrap a function that returns a promise and may throw:The Thenable Behavior
ResultAsync implements PromiseLike<Result<T, E>>, which means it’s “thenable” and can be used with await and .then():
Using with async/await
Using with .then()
Mixing with Promise.all
Methods: Sync vs Async Parameters
ManyResultAsync methods accept both synchronous and asynchronous functions:
map - Synchronous or Asynchronous
mapErr - Transform Errors
andThen - Chain Dependent Operations
andThen accepts both Result and ResultAsync return types, automatically handling the conversion.Chaining Operations
One of the most powerful features ofResultAsync 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:orTee - Side Effects on Error
Execute a side effect on error without changing the error:Advanced Patterns
Pattern 1: Parallel Operations with combine
Pattern 2: Collecting All Errors
Pattern 3: Sequential with Error Recovery
Pattern 4: Transform and Unwrap
Comparison: Promise of Result vs ResultAsync
- Promise of Result
- 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