-
Notifications
You must be signed in to change notification settings - Fork 0
Class.ResultAsync
resultar / ResultAsync
Defined in: result-async.ts:67
Represents an asynchronous Result type that wraps a Promise of a Result<T, E>. This class provides a way to handle asynchronous operations that may succeed with a value of type T or fail with an error of type E.
// Create a successful async result
const okAsync = ResultAsync.okAsync(42);
// Create a failed async result
const errAsync = ResultAsync.errAsync(new Error("Something went wrong"));
// Transform a Promise into a ResultAsync
const resultFromPromise = ResultAsync.fromPromise(
fetch("https://api.example.com/data"),
(error) => new Error(`API call failed: ${error}`)
);ResultAsync implements the PromiseLike interface, allowing it to be used with async/await syntax. It provides various utility methods for transforming and combining results, similar to the Result type, but operating in an asynchronous context.
The class includes methods for:
- Creating ResultAsync instances (okAsync, errAsync, fromPromise)
- Transforming values (map, mapErr)
- Chaining operations (andThen, orElse)
- Error handling (tapError)
- Conditional branching (if)
- Combining multiple ResultAsync instances (combine, combineWithAllErrors)
T
The type of the success value
E
The type of the error value
-
PromiseLike<Result<T,E>>
new ResultAsync<
T,E>(res):ResultAsync<T,E>
Defined in: result-async.ts:227
Promise<Result<T, E>>
ResultAsync<T, E>
[asyncIterator]():
AsyncGenerator<Result<never,E>,T>
Defined in: result-async.ts:424
AsyncGenerator<Result<never, E>, T>
andThen<
R>(f):ResultAsync<InferOkTypes<R>,E|InferErrTypes<R>>
Defined in: result-async.ts:262
R extends Result<unknown, unknown>
(t) => R
ResultAsync<InferOkTypes<R>, E | InferErrTypes<R>>
andThen<
R>(f):ResultAsync<InferAsyncOkTypes<R>,E|InferAsyncErrTypes<R>>
Defined in: result-async.ts:265
R extends ResultAsync<unknown, unknown>
(t) => R
ResultAsync<InferAsyncOkTypes<R>, E | InferAsyncErrTypes<R>>
andThen<
U,F>(f):ResultAsync<U,E|F>
Defined in: result-async.ts:268
U
F
(t) => Result<U, F> | ResultAsync<U, F>
ResultAsync<U, E | F>
finally(
f):DisposableResultAsync<T,E>
Defined in: result-async.ts:409
(value, error) => void
DisposableResultAsync<T, E>
if(
fCondition):object
Defined in: result-async.ts:285
(t) => boolean
object
true: <
X1,Y1>(fTrue) =>object
X1
Y1
(t) => ResultAsync<X1, Y1>
object
false: <
X2,Y2>(fFalse) =>ResultAsync<X1|X2,E|Y1|Y2>
X2
Y2
(t) => ResultAsync<X2, Y2>
ResultAsync<X1 | X2, E | Y1 | Y2>
log(
f):ResultAsync<T,E>
Defined in: result-async.ts:344
Performs a side effect for the Ok variant of ResultAsync.
This function can be used for control flow based on result values.
(t?, e?) => void | Promise<void>
The function to call if the result is Ok
ResultAsync<T, E>
self if the result is Err, otherwise the result of f
map<
X>(f):ResultAsync<X,E>
Defined in: result-async.ts:248
X
(t) => X | Promise<X>
ResultAsync<X, E>
mapErr<
U>(f):ResultAsync<T,U>
Defined in: result-async.ts:238
U
(t) => U | Promise<U>
ResultAsync<T, U>
match<
A,B>(ok,fnErr):Promise<A|B>
Defined in: result-async.ts:326
A
B = A
(t) => A
(e) => B
Promise<A | B>
orElse<
R>(f):ResultAsync<T|InferOkTypes<R>,InferErrTypes<R>>
Defined in: result-async.ts:305
R extends Result<unknown, unknown>
(e) => R
ResultAsync<T | InferOkTypes<R>, InferErrTypes<R>>
orElse<
R>(f):ResultAsync<T|InferAsyncOkTypes<R>,InferAsyncErrTypes<R>>
Defined in: result-async.ts:308
R extends ResultAsync<unknown, unknown>
(e) => R
ResultAsync<T | InferAsyncOkTypes<R>, InferAsyncErrTypes<R>>
orElse<
U,A>(f):ResultAsync<T|U,A>
Defined in: result-async.ts:311
U
A
(e) => Result<U, A> | ResultAsync<U, A>
ResultAsync<T | U, A>
safeUnwrap():
AsyncGenerator<Result<never,E>,T>
Defined in: result-async.ts:446
AsyncGenerator<Result<never, E>, T>
will be removed in 2.0.0.
You can use safeTry without this method.
safeTry(async function* () {
const okValue = yield* yourResult
})Emulates Rust's ? operator in safeTry's body. See also safeTry.
tap(
f):ResultAsync<T,E>
Defined in: result-async.ts:370
Performs a side effect for the Ok variant of ResultAsync.
This function can be used for control flow based on result values.
(t) => void | Promise<void>
The function to call if the result is Ok
ResultAsync<T, E>
self if the result is Err, otherwise the result of f
tapError(
f):ResultAsync<T,E>
Defined in: result-async.ts:393
Performs a side effect for the Err variant of ResultAsync.
This function can be used for control flow based on result values.
(e) => void | Promise<void>
The function to call if the result is Err
ResultAsync<T, E>
self if the result is Ok, otherwise the result of f
then<
A,B>(successCallback?,failureCallback?):PromiseLike<A|B>
Defined in: result-async.ts:231
Attaches callbacks for the resolution and/or rejection of the Promise.
A
B
(res) => A | PromiseLike<A>
(reason) => B | PromiseLike<B>
PromiseLike<A | B>
A Promise for the completion of which ever callback is executed.
PromiseLike.then
unwrapOr<
A>(t):Promise<T|A>
Defined in: result-async.ts:330
A
A
Promise<T | A>
unwrapOrThrow():
Promise<T>
Defined in: result-async.ts:334
Promise<T>
staticcombine<T>(asyncResultList):CombineResultAsyncs<T>
Defined in: result-async.ts:172
T extends readonly [ResultAsync<unknown, unknown>, ResultAsync<unknown, unknown>]
T
CombineResultAsyncs<T>
staticcombine<T>(asyncResultList):CombineResultAsyncs<T>
Defined in: result-async.ts:175
T extends readonly ResultAsync<unknown, unknown>[]
T
CombineResultAsyncs<T>
staticcombineWithAllErrors<T>(asyncResultList):CombineResultsWithAllErrorsArrayAsync<T>
Defined in: result-async.ts:184
T extends readonly [ResultAsync<unknown, unknown>, ResultAsync<unknown, unknown>]
T
CombineResultsWithAllErrorsArrayAsync<T>
staticcombineWithAllErrors<T>(asyncResultList):CombineResultsWithAllErrorsArrayAsync<T>
Defined in: result-async.ts:187
T extends readonly ResultAsync<unknown, unknown>[]
T
CombineResultsWithAllErrorsArrayAsync<T>
staticerrAsync<T,E>(err):ResultAsync<T,E>
Defined in: result-async.ts:96
Returns a ResultAsync instance that is immediately resolved with a Result.err(error).
T = never
E = unknown
E
ResultAsync<T, E>
A ResultAsync instance with the given error and value type T.
staticerrAsync<T,E>(err):ResultAsync<T,void>
Defined in: result-async.ts:98
Returns a ResultAsync instance that is immediately resolved with a Result.err(error).
T = never
E extends void = void
void
ResultAsync<T, void>
A ResultAsync instance with the given error and value type T.
staticfromPromise<T,E>(promise,errorFn):ResultAsync<T,E>
Defined in: result-async.ts:163
Returns a ResultAsync instance that is resolved with a Result.ok(value) or Result.err(error) based on the provided promise.
T
E
PromiseLike<T>
The promise to be wrapped in a ResultAsync.
(e) => E
A function that transforms the error from the promise into the error type E.
ResultAsync<T, E>
A ResultAsync instance with the given promise and error type E.
staticfromSafePromise<T,E>(promise):ResultAsync<T,E>
Defined in: result-async.ts:147
Returns a ResultAsync instance that is resolved with a Result.ok(value) or Result.err(error) based on the provided promise.
T
E = never
PromiseLike<T>
The promise to be wrapped in a ResultAsync.
ResultAsync<T, E>
A ResultAsync instance with the given promise and error type E.
staticfromThrowable<A,T,E>(fn,errorFn?): (...args) =>ResultAsync<T,E>
Defined in: result-async.ts:207
Wraps a async function with a try catch, creating a new function with the same
arguments but returning Ok if successful, Err if the function throws
A extends readonly any[]
T
E
(...args) => Promise<T>
function to wrap with ok on success or err on failure
(err) => E
when an error is thrown, this will wrap the error result if provided
a new function that returns a ResultAsync
(...
args):ResultAsync<T,E>
...A
ResultAsync<T, E>
staticokAsync<T,E>(value):ResultAsync<T,E>
Defined in: result-async.ts:74
Returns a ResultAsync instance that is immediately resolved with a Result.ok(value).
T
E = never
T
The value to be wrapped in a Result.ok.
ResultAsync<T, E>
A ResultAsync instance with the given value and error type E.
staticokAsync<T,E>(value):ResultAsync<void,E>
Defined in: result-async.ts:76
Returns a ResultAsync instance that is immediately resolved with a Result.ok(value).
T extends void = void
E = never
void
The value to be wrapped in a Result.ok.
ResultAsync<void, E>
A ResultAsync instance with the given value and error type E.
statictryCatch<T,E>(fn,errorFn?):ResultAsync<T,E>
Defined in: result-async.ts:124
Creates a ResultAsync from a Promise, catching any errors that occur during its execution.
T
E
The Promise or a function returning a Promise to be wrapped in a ResultAsync.
Promise<T> | () => Promise<T>
(e) => E
Optional function to transform the caught error into a specific error type. If not provided, the original error will be used.
ResultAsync<T, E>
A ResultAsync that will resolve to Ok with the promise's value if successful, or Err with either the transformed error (if errorFn is provided) or the original error.
// Without error transformer
const result = ResultAsync.tryCatch(Promise.resolve(42));
// With error transformer
const result = ResultAsync.tryCatch(
fetch('https://api.example.com'),
(error) => new CustomError('API request failed')
);
staticunitAsync<E>():ResultAsync<undefined,E>
Defined in: result-async.ts:86
Returns a ResultAsync that is immediately resolved with a Result.ok(undefined) value.
E = never
ResultAsync<undefined, E>
A ResultAsync instance with undefined as the value type and E as the error type.