ConstWraps a Promise that might throw into a Result.
Catches any errors from the promise and converts them to Failure with DomainError.
The promise to wrap
OptionalerrorMapper: (error: unknown) => DomainErrorOptional function to map error to DomainError
Promise resolving to Success or Failure
Maps over an async Result, transforming the success value.
If the Result is a Failure, it's returned as-is. The mapping function can be sync or async.
Promise of mapped Result
Flat maps over an async Result, chaining async operations that return Results.
If the Result is a Failure, it's returned as-is without calling the function. Useful for chaining async operations that can fail.
Promise of Result to flat map over
Function returning Promise of Result
Promise of final Result
Executes multiple async Results in sequence, collecting all results.
Similar to Results.sequence but for async operations. Short-circuits on first failure.
Array of async operation functions
Promise of Result with array of all success values
Executes multiple async Results in parallel, collecting all results.
Similar to Results.parallel but waits for all promises. Returns first encountered failure, or success with all values.
Array of async operation functions
Promise of Result with array of all success values
// Wrap async operation that might throw
const result = await AsyncResults.fromPromise(
fetch('/api/users'),
(error) => new DomainError('FETCH_ERROR', String(error))
);
// Chain async operations
const finalResult = await AsyncResults.flatMap(
AsyncResults.fromPromise(getUserId()),
async (userId) => getUserData(userId)
);
Helpers for working with async operations that return Results.
These helpers simplify common patterns when dealing with Promises and Results, reducing boilerplate code and improving error handling.