Skip to main content

Introduction to ResultAsync

ResultAsync wraps a Promise<Result<T, E>> and provides the same API as Result, allowing you to chain operations without awaiting at each step:

Creating ResultAsync

From Safe Promises

Use fromSafePromise for promises that never reject:

From Promises with Error Handling

Use fromPromise to convert promises that might reject:

From Throwable Functions

Create a safe wrapper for functions that might throw:
fromThrowable is safer than fromPromise because it catches both synchronous throws (before the promise is created) and asynchronous rejections.

Chaining Async Operations

Using map with Async Functions

Both sync and async functions work with map:

Using andThen for Async Chains

andThen works with both Result and ResultAsync:

Real-World Example: API Request Pipeline

1

Define the data types

2

Create helper functions

3

Compose the pipeline

Async Error Mapping

mapErr also supports async functions:

Combining Sync and Async Results

From Result to ResultAsync

Use asyncAndThen or asyncMap on a regular Result:

From ResultAsync to Result

Simply await the ResultAsync:

Using with async/await

ResultAsync is thenable, so it works seamlessly with async/await:

Error Recovery with orElse

Recover from errors asynchronously:

Side Effects with andTee

Perform side effects without affecting the result:
Even if the side effect throws, the result is unaffected:

Pattern: Parallel Async Operations

Use combine for multiple async operations (covered in detail in Combining Results):

Converting Throwable Code

Wrap existing promise-based code:

Next Steps

Combining Results

Learn how to work with multiple Results using combine

Error Recovery

Master error handling with orElse, match, and unwrapOr