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
UsefromSafePromise for promises that never reject:
From Promises with Error Handling
UsefromPromise 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 withmap:
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
UseasyncAndThen or asyncMap on a regular Result:
From ResultAsync to Result
Simply await theResultAsync:
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:Pattern: Parallel Async Operations
Usecombine for multiple async operations (covered in detail in Combining Results):
Converting Throwable Code
Wrap existing promise-based code:- Before (throws)
- After (safe)
Next Steps
Combining Results
Learn how to work with multiple Results using combine
Error Recovery
Master error handling with orElse, match, and unwrapOr