isResult
Check if a value is a Result tuple
217 bytes
Usage
Check if a value is a Result
tuple.
Don’t know what that is? Read the Result section further down.
Also see the related isResultOk and isResultErr functions.
Types In-Depth
Result
“Results” are tuples of 2 elements (an error and a result value).
- The first element is always the error, or
undefined
if the operation was successful. - The second element is always the result value, unless an error occurred.
- These tuples are represented by the
Result<TResult, TError>
type. - A default error type of
Error
is used when no error type is explicitly defined (e.g.Result<string>
).
Ok and Err
There are 2 types of result: Ok<TResult>
and Err<TError>
.
- The
Ok
type represents a successful operation. It’s a tuple of[undefined, TResult]
. - The
Err
type represents a failed operation. It’s a tuple of[TError, undefined]
.
The names “Ok” and “Err” are inspired by Rust’s std::result
module.
To check for an Ok
result, do this:
To check for an Err
result, do this:
You can also use the isResultOk
and isResultErr
functions to check for Ok
and Err
results respectively.