The type of the ok value
The type of the error (must extend Error)
Creates a new AsyncResult from a Promise that resolves to a Result.
While result.try_async()
is the preferred way to create AsyncResult instances,
the constructor is useful when you have an async function that already returns
a Promise<Result>
. Wrapping it with new AsyncResult()
provides access to
the chaining API without requiring await
and without "coloring" your function
as async. This is particularly beneficial in contexts where await
isn't allowed,
such as top-level code or in component logic of some frontend frameworks.
A Promise that resolves to a Result
// Async function that returns Promise<Result>
async function fetchUserData(id: string): Promise<Result<User, Error>> {
return result.try_async(() => fetch(`/api/users/${id}`))
.then(response => response.json());
}
// Without AsyncResult constructor: requires await, makes function async
async function processUserAsync(id: string): Promise<Result<string, Error>> {
const userData = await fetchUserData(id);
return userData.map(user => user.name.toUpperCase());
}
// With AsyncResult constructor: no await needed, function stays sync
function processUserSync(id: string): AsyncResult<string, Error> {
return new AsyncResult(fetchUserData(id))
.map(user => user.name.toUpperCase());
}
// Both usage patterns work the same way:
const result1 = await processUserAsync("123");
const result2 = await processUserSync("123");
PromiseLike implementation equivalent to Promise.then. Allows AsyncResult to be awaited.
The type returned when the promise resolves
The type returned when the promise rejects
A PromiseLike that resolves to the result of the executed callback
Returns the contained value if Ok, otherwise returns the provided default value.
The value to return if the Result contains an error
A Promise resolving to the contained value or the default value
Transform the contained value if Ok, otherwise return the error unchanged.
The type of the transformed value
Function to transform the value if Ok
A new AsyncResult with the transformed value if Ok, otherwise the original error
Transform the contained error if Error, otherwise return the value unchanged.
The type of the transformed error
Function to transform the error if Error
A new AsyncResult with the transformed error if Error, otherwise the original value
Pattern match against the Result, executing the appropriate callback and returning its result.
The return type of the on_ok callback
The return type of the on_error callback
Object containing callback functions for Ok and Error cases
Function to execute if Result is Ok, receiving the value
Function to execute if Result is Error, receiving the error
A Promise resolving to the result of the executed callback
Chain another Result-returning operation if this Result is Ok. If this Result is Error, the function is not called and the error is propagated.
The type of the value in the returned Result
Function that takes the Ok value and returns a new Result
A new AsyncResult with the result returned by fn if Ok, otherwise the original error
Provide a fallback Result if this Result is Error. If this Result is Ok, the function is not called and the value is preserved.
The type of error in the fallback Result
Function that takes the Error and returns a fallback Result
A new AsyncResult with the fallback Result returned by fn if Error, otherwise the original Ok value
Executes an async function with the contained value if Ok, and wraps the result in a Result. If this AsyncResult is Error, the function is not called and the error is propagated. If the async function throws or rejects, the error is caught and wrapped in a TryAsyncError with detailed debugging information.
The type of the value returned by the async function
Async function that takes the Ok value and returns a Promise
A new AsyncResult containing the result of the async function or a union of the original error and TryAsyncError
// Chain async operations with rich error information
const userData = await result.try_async(() => fetch('/api/user/123'))
.try_async(response => response.json()) // Creates TryAsyncError if JSON parsing fails
.map(user => user.name)
.match(
name => console.log(`User: ${name}`),
error => {
if (error instanceof TryAsyncError) {
console.log(`Operation "${error.operation}" failed`);
console.log(`Original error:`, error.originalError);
}
}
);
Executes an async function with the contained value if Ok, and wraps the result in a Result. If this AsyncResult is Error, the function is not called and the error is propagated. If the async function throws or rejects, the error is caught and passed to the provided error mapper.
The type of the value returned by the async function
The type of the error returned by the error mapper
Async function that takes the Ok value and returns a Promise
Function that maps the caught error to a specific error type
A new AsyncResult containing the result of the async function or a union of the original error and mapped error
Async iterator support. Yields the contained value if Ok, nothing if Error.
An async generator that yields the value if Ok, otherwise yields nothing
// Iterate over successful values
for await (const value of result.try_async(() => fetchUser("123"))) {
console.log(value.name); // Only executes if fetch succeeds
}
// Collect successful values from multiple async operations
const users = [];
for await (const user of result.try_async(() => fetchUser("456"))) {
users.push(user);
}
An awaitable wrapper for Result that enables immediate method chaining on async operations. AsyncResult implements PromiseLike and provides Result transformation methods like map, and_then, and or_else. The AsyncResult must be awaited to inspect the final result with
is_ok()
oris_error()
.Example