-
Notifications
You must be signed in to change notification settings - Fork 0
Class.Result
resultar / Result
A Result
is a type that represents either success (Ok
) or failure (Err
).
It is often used to replace exceptions or null
returns.
Result
is often used to handle errors in a functional way.
The Result
type is often used to handle errors in a functional way.
• T
• E
-
Resultable
<T
,E
>
readonly
error:E
Resultable.error
readonly
value:T
Resultable.value
_unsafeUnwrap(
config
?):T
This method is unsafe, and should only be used in a test environments
Takes a Result<T, E>
and returns a T
when the result is an Ok
, otherwise it throws a custom object.
• config?: ErrorConfig
T
Resultable._unsafeUnwrap
_unsafeUnwrapErr(
config
?):E
This method is unsafe, and should only be used in a test environments
takes a Result<T, E>
and returns a E
when the result is an Err
,
otherwise it throws a custom object.
• config?: ErrorConfig
E
Resultable._unsafeUnwrapErr
[iterator]():
Generator
<Result
<never
,E
>,T
,any
>
Generator
<Result
<never
, E
>, T
, any
>
andThen<
X
,Y
>(f
):Result
<X
,E
|Y
>
Similar to map
Except you must return a new Result
.
This is useful for when you need to do a subsequent computation using the
inner T
value, but that computation might fail.
Additionally, andThen
is really useful as a tool to flatten a
Result<Result<A, E2>, E1>
into a Result<A, E2>
(see example below).
• X
• Y
• f
The function to apply to the current value
Result
<X
, E
| Y
>
asyncAndThen<
X
,Y
>(f
):ResultAsync
<X
,E
|Y
>
Similar to map
Except you must return a new Result
.
This is useful for when you need to do a subsequent async computation using
the inner T
value, but that computation might fail. Must return a ResultAsync
• X
• Y
• f
The function that returns a ResultAsync
to apply to the current
value
ResultAsync
<X
, E
| Y
>
asyncMap<
X
>(f
):ResultAsync
<X
,E
>
Maps a Result<T, E>
to ResultAsync<U, E>
by applying an async function to a contained Ok
value, leaving an Err
value untouched.
• X
• f
An async function to apply an OK
value
ResultAsync
<X
, E
>
finally<
X
,Y
>(f
):DisposableResult
<X
,Y
>
This method is used to clean up and release any resources that the Result
• X = T
• Y = E
• f
The function that will be called to clean up the resources
DisposableResult
<X
, Y
>
if(
fCondition
):object
• fCondition
object
true: <
X1
,Y1
>(fTrue
) =>object
• X1
• Y1
• fTrue
object
false: <
X2
,Y2
>(fFalse
) =>Result
<X1
|X2
,E
|Y1
|Y2
>
• X2
• Y2
• fFalse
Result
<X1
| X2
, E
| Y1
| Y2
>
isErr():
boolean
Used to check if a Result
is an Err
boolean
true
if the result is an Err
variant of Result
Resultable.isErr
isOk():
boolean
Used to check if a Result
is an OK
boolean
true
if the result is an OK
variant of Result
Resultable.isOk
log(
fn
):this
Performs a side effect for the Ok
or Err
variant of Result
.
• fn
The function to apply an OK
value
this
the result of applying f
or an Err
untouched
map<
X
>(f
):Result
<X
,E
>
Maps a Result<T, E>
to Result<U, E>
by applying a function to a contained Ok
value, leaving an Err
value
untouched.
• X
• f
The function to apply an OK
value
Result
<X
, E
>
the result of applying f
or an Err
untouched
mapErr<
X
>(fn
):Result
<T
,X
>
Maps a Result<T, E>
to Result<T, F>
by applying a function to a
contained Err
value, leaving an Ok
value untouched.
This function can be used to pass through a successful result while handling an error.
• X
• fn
Result
<T
, X
>
match<
A
,B
>(fnOk
,fnErr
):A
|B
Given 2 functions (one for the Ok
variant and one for the Err
variant)
execute the function that matches the Result
variant.
Match callbacks do not necessitate to return a Result
, however you can
return a Result
if you want to.
match
is like chaining map
and mapErr
, with the distinction that
with match
both functions must have the same return type.
• A
• B = A
• fnOk
• fnErr
A
| B
orElse<
R
>(f
):Result
<T
|InferOkTypes
<R
>,InferErrTypes
<R
>>
Takes an Err
value and maps it to a Result<T, SomeNewType>
.
This is useful for error recovery.
• R extends Result
<unknown
, unknown
>
• f
A function to apply to an Err
value, leaving Ok
values
untouched.
Result
<T
| InferOkTypes
<R
>, InferErrTypes
<R
>>
safeUnwrap():
Generator
<Result
<never
,E
>,T
,any
>
Generator
<Result
<never
, E
>, T
, any
>
tap(
fn
):this
Performs a side effect for the Ok
variant of Result
.
• fn
The function to apply an OK
value
this
the result of applying f
or an Err
untouched
tapError(
fn
):this
Performs a side effect for the Err
variant of Result
.
• fn
The function to apply an Err
value
this
the result of applying f
or an Ok
untouched
unwrapOr<
A
>(defaultValue
):T
|A
Unwrap the Ok
value, or return the default if there is an Err
• A
• defaultValue: A
T
| A
Resultable.unwrapOr
unwrapOrThrow():
T
T
static
combine<T
>(resultList
):CombineResults
<T
>
• T extends readonly [Result
<unknown
, unknown
>, Result
<unknown
, unknown
>]
• resultList: T
CombineResults
<T
>
static
combine<T
>(resultList
):CombineResults
<T
>
• T extends readonly Result
<unknown
, unknown
>[]
• resultList: T
CombineResults
<T
>
static
combineWithAllErrors<T
>(resultList
):CombineResultsWithAllErrorsArray
<T
>
• T extends readonly [Result
<unknown
, unknown
>, Result
<unknown
, unknown
>]
• resultList: T
CombineResultsWithAllErrorsArray
<T
>
static
combineWithAllErrors<T
>(resultList
):CombineResultsWithAllErrorsArray
<T
>
• T extends readonly Result
<unknown
, unknown
>[]
• resultList: T
CombineResultsWithAllErrorsArray
<T
>
static
err<T
,E
>(error
):Result
<T
,E
>
• T = never
• E = unknown
• error: E
Result
<T
, E
>
static
fromThrowable<Fn
,E
>(fn
,errorFn
?): (...args
) =>Result
<ReturnType
<Fn
>,E
>
Wraps a function with a try catch, creating a new function with the same
arguments but returning Ok
if successful, Err
if the function throws
• Fn extends (...args
) => unknown
• E
• fn: Fn
function to wrap with ok on success or err on failure
• errorFn?
when an error is thrown, this will wrap the error result if provided
Function
• ...args: Parameters
<Fn
>
Result
<ReturnType
<Fn
>, E
>
static
ok<T
,E
>(value
):Result
<T
,E
>
Creates a new Result
instance representing a successful operation.
• T
• E = never
• value: T
The value to be wrapped in the Result
instance.
Result
<T
, E
>
A new Result
instance with the provided value.
static
unit<E
>():Result
<undefined
,E
>
Creates a new Result
instance representing a successful operation with an undefined value.
• E = never
Result
<undefined
, E
>
A new Result
instance with an undefined value.