API reference / @backpack/error-handling / async-result / AsyncResult
Class: AsyncResult<T>
Extension of a Promise<T>
, providing some nice additional operators for improved error handling.
Extends
Promise
<T
>
Type Parameters
Type Parameter | Description |
---|---|
T | Represents the type of the resulting value, if successful. |
Constructors
Constructor
new AsyncResult<T>(executor): AsyncResult<T>;
Creates a new AsyncResult.
Parameters
Parameter | Type |
---|---|
executor | (resolve , reject ) => void |
Returns
AsyncResult
<T
>
Overrides
Promise<T>.constructor
Methods
asResult()
asResult(): Promise<Result<T, unknown>>;
Converts the AsyncResult<T>
into a Promise<Result<T, unknown>>
.
Can be used in combination with async/await to handle the Result
in a more imperative style.
Returns
Promise
<Result
<T
, unknown
>>
asResultCatchingOnly()
asResultCatchingOnly<T1, T2>(e, ...exceptions): Promise<Result<T, T1 | T2 extends Class<E>[] ? E : unknown>>;
Converts the AsyncResult<T>
into a Promise<Result<T, unknown>>
, catching only errors that are an instance of the error classes provided.
Can be used in combination with async/await to handle the Result
in a more imperative style.
Type Parameters
Type Parameter |
---|
T1 |
T2 extends Class [] |
Parameters
Parameter | Type |
---|---|
e | Class <T1 > |
...exceptions | T2 |
Returns
Promise
<Result
<T
, T1
| T2
extends Class
<E
>[] ? E
: unknown
>>
map()
map<TResult>(mapFn): AsyncResult<TResult>;
If successful, maps the value, catching any errors in the process.
Type Parameters
Type Parameter | Default type |
---|---|
TResult | T |
Parameters
Parameter | Type |
---|---|
mapFn | (value ) => TResult | PromiseLike <TResult > |
Returns
AsyncResult
<TResult
>
match()
match<TResult1, TResult2>(onSuccess, onError): AsyncResult<TResult1 | TResult2>;
Maps the value if successful, or else the error if failed.
For example, .match(x => 1, e => 2)
would be similar to .map(x => 1).recover(e => 2)
Type Parameters
Type Parameter |
---|
TResult1 |
TResult2 |
Parameters
Parameter | Type |
---|---|
onSuccess | (value ) => TResult1 | PromiseLike <TResult1 > |
onError | (error ) => TResult2 | PromiseLike <TResult2 > |
Returns
AsyncResult
<TResult1
| TResult2
>
onFailure()
onFailure(errorConsumer): AsyncResult<T>;
Executes the given callback if failed, letting the original error pass through.
Can be used to introduce a side effect (such as logging the error).
Parameters
Parameter | Type |
---|---|
errorConsumer | (error ) => void |
Returns
AsyncResult
<T
>
onFailureIfInstanceOf()
onFailureIfInstanceOf<TNarrowedError>(errorClass, errorConsumer): AsyncResult<T>;
Executes the given callback if failed, if the error is an instance of the class provided, letting the original error pass through.
Can be used to introduce a side effect, such as logging.
Type Parameters
Type Parameter |
---|
TNarrowedError |
Parameters
Parameter | Type |
---|---|
errorClass | Class <TNarrowedError > |
errorConsumer | (error ) => void |
Returns
AsyncResult
<T
>
onSuccess()
onSuccess(consumer): AsyncResult<T>;
Executes the given callback if successful, letting the original value pass through.
Can be used to introduce a side effect.
Parameters
Parameter | Type |
---|---|
consumer | (value ) => void |
Returns
AsyncResult
<T
>
orElse()
orElse<TResult>(defaultValue): AsyncResult<T | TResult>;
If failed, maps the error to the given value, to recover into a successful AsyncResult
.
Alias for .recover(() => value)
to improve semantics.
Type Parameters
Type Parameter |
---|
TResult |
Parameters
Parameter | Type |
---|---|
defaultValue | TResult | PromiseLike <TResult > |
Returns
AsyncResult
<T
| TResult
>
orElseGet()
orElseGet<TResult>(defaultValueProvider): AsyncResult<T | TResult>;
If failed, maps the error to the value returned by the given provider, to recover into a successful AsyncResult
.
Alias for .recover(() => value)
to improve semantics.
Type Parameters
Type Parameter |
---|
TResult |
Parameters
Parameter | Type |
---|---|
defaultValueProvider | (error ) => TResult | PromiseLike <TResult > |
Returns
AsyncResult
<T
| TResult
>
recover()
recover<TResult>(recoverFn): AsyncResult<T | TResult>;
If failed, maps the error to another value, to recover into a successful AsyncResult
.
Type Parameters
Type Parameter |
---|
TResult |
Parameters
Parameter | Type |
---|---|
recoverFn | (error ) => TResult | PromiseLike <TResult > |
Returns
AsyncResult
<T
| TResult
>
recoverIf()
Call Signature
recoverIf<TResult, TNarrowedError>(predicate, recoverFn): AsyncResult<TResult>;
If failed, maps the error to another value if it matches the given type guard, to recover into a successful AsyncResult
.
Type Parameters
Type Parameter |
---|
TResult |
TNarrowedError |
Parameters
Parameter | Type |
---|---|
predicate | (error ) => error is TNarrowedError |
recoverFn | (error ) => TResult | PromiseLike <TResult > |
Returns
AsyncResult
<TResult
>
Call Signature
recoverIf<TResult>(predicate, recoverFn): AsyncResult<TResult>;
If failed, maps the error to another value if it matches the given predicate, to recover into a successful AsyncResult
.
Type Parameters
Type Parameter |
---|
TResult |
Parameters
Parameter | Type |
---|---|
predicate | (error ) => boolean |
recoverFn | (error ) => TResult | PromiseLike <TResult > |
Returns
AsyncResult
<TResult
>
recoverIfInstanceOf()
recoverIfInstanceOf<TResult, TNarrowedError>(errorClass, recoverFn): AsyncResult<T | TResult>;
If failed, maps the error to a value if it is an instance of the provided error class, to recover into a successful AsyncResult
.
Type Parameters
Type Parameter |
---|
TResult |
TNarrowedError |
Parameters
Parameter | Type |
---|---|
errorClass | Class <TNarrowedError > |
recoverFn | (error ) => TResult | PromiseLike <TResult > |
Returns
AsyncResult
<T
| TResult
>
failure()
static failure<T>(error): AsyncResult<Awaited<T>>;
Creates an AsyncResult
representing a failure.
Alias for AsyncResult.reject(error)
, to improve semantics.
Type Parameters
Type Parameter | Default type |
---|---|
T | never |
Parameters
Parameter | Type |
---|---|
error | unknown |
Returns
AsyncResult
<Awaited
<T
>>
fromPromise()
static fromPromise<T>(value): AsyncResult<T>;
Creates an AsyncResult<T>
out of an existing Promise<T>
.
Type Parameters
Type Parameter |
---|
T |
Parameters
Parameter | Type |
---|---|
value | PromiseLike <T > |
Returns
AsyncResult
<T
>
reject()
static reject<T>(error): AsyncResult<Awaited<T>>;
Type Parameters
Type Parameter | Default type |
---|---|
T | never |
Parameters
Parameter | Type |
---|---|
error | unknown |
Returns
AsyncResult
<Awaited
<T
>>
Inherit Doc
Overrides
Promise.reject
resolve()
Call Signature
static resolve(): AsyncResult<void>;
Returns
AsyncResult
<void
>
Inherit Doc
Overrides
Promise.resolve
Call Signature
static resolve<T>(value): AsyncResult<Awaited<T>>;
Creates a new resolved promise for the provided value.
Type Parameters
Type Parameter |
---|
T |
Parameters
Parameter | Type | Description |
---|---|---|
value | T | A promise. |
Returns
AsyncResult
<Awaited
<T
>>
A promise whose internal state matches the provided promise.
Overrides
Promise.resolve
Call Signature
static resolve<T>(value): AsyncResult<Awaited<T>>;
Creates a new resolved promise for the provided value.
Type Parameters
Type Parameter |
---|
T |
Parameters
Parameter | Type | Description |
---|---|---|
value | T | PromiseLike <T > | A promise. |
Returns
AsyncResult
<Awaited
<T
>>
A promise whose internal state matches the provided promise.
Overrides
Promise.resolve
success()
Call Signature
static success(): AsyncResult<void>;
Creates an AsyncResult
representing a successful value.
Alias for AsyncResult.resolve(value)
, to improve semantics.
Returns
AsyncResult
<void
>
Call Signature
static success<T>(value): AsyncResult<Awaited<T>>;
Creates an AsyncResult
representing a successful value.
Alias for AsyncResult.resolve(value)
, to improve semantics.
Type Parameters
Type Parameter |
---|
T |
Parameters
Parameter | Type |
---|---|
value | T |
Returns
AsyncResult
<Awaited
<T
>>
Call Signature
static success<T>(value): AsyncResult<Awaited<T>>;
Creates an AsyncResult
representing a successful value.
Alias for AsyncResult.resolve(value)
, to improve semantics.
Type Parameters
Type Parameter |
---|
T |
Parameters
Parameter | Type |
---|---|
value | T | PromiseLike <T > |
Returns
AsyncResult
<Awaited
<T
>>
try()
static try<T>(block): AsyncResult<T>;
Executes a callback, wrapping its result in a AsyncResult<T>
.
Similar to Promise.try()
(currently stage 3).
Type Parameters
Type Parameter | Description |
---|---|
T | Represents the type of the resulting value, if successful. |
Parameters
Parameter | Type | Description |
---|---|---|
block | () => T | PromiseLike <T > | The callback to execute. Any synchronous or asynchronous error will result in a rejected AsyncResult . |
Returns
AsyncResult
<T
>
See
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/try