The type of the success value
The type of the error value
Returns true if both results are Ok and their success values are equal using
the JavaScript == operator, or if both results are Err and their error values
are equal using the JavaScript == operator. If the inner values are Results or
Options, they will be compared using their own equals method.
Ok(1).equals(Ok(1)); // true
Ok(Ok(1)).equals(Ok(Ok(1))); // true
Ok(Some(1)).equals(Ok(Some(1))); // true
Ok(Err(1)).equals(Ok(Err(1))); // true
Err(1).equals(Err(1)); // true
Err(1).equals(Ok(1)); // false
@param other - The result to compare against.
@returns {boolean} Whether the results are equal.
@group Equality
Returns true if both results are Ok and their success values are equal using
the JavaScript === operator, or if both results are Err and their error values
are equal using the JavaScript === operator. If the inner values are Results or
Options, they will be compared using their own strictEquals method.
Calls fn with the success value if the result is Ok, otherwise does nothing.
Returns the original result.
Calls fn with the error value if the result is Err, otherwise does nothing.
Returns the original result.
Returns true if the result is Err.
true if the result is Err, false otherwise.
Returns true if the result is Err and the given function returns true.
Returns true if the result is Ok.
true if the result is Ok, false otherwise.
Returns true if the result is Ok and the given function returns true.
Returns other if the result is Ok, otherwise returns the current error.
Returns None if the result is Err, otherwise calls fn with the success value and returns the result.
Converts the Result<T, E> into an Option<E>, mapping Ok(_) to None
and Err(e) to Some(e).
Converts from Result<Result<T, E>, E> to Result<T, E>. Only one level of nesting is removed.
Maps a Result<T, E> to Result<U, E> by applying the function to the success value.
Maps a Result<T, E> to Result<T, U> by applying the function to the error value.
Returns a result wrapping the provided defaultValue if the result is Err,
or calls fn with the success value and returns a new result wrapping its return value.
The type of the new success value.
The value to use if the result is Err.
A function that takes the success value and returns a new value.
A new result containing either the default or transformed value.
Returns a result wrapping the return value of defaultFn if the result is Err,
or calls mapFn with the success value and returns a new result wrapping its return value.
This function is useful for providing a default result that is expensive to compute or has side effects.
The type of the new success value.
A function that returns the default value if the result is Err.
A function that takes the success value and returns a new value.
A new result containing either the default or transformed value.
Converts the Result<T, E> into an Option<T>, mapping Ok(v) to Some(v)
and Err(_) to None.
Returns the current result if it is Ok, otherwise returns other.
Returns the current result if it is Ok, otherwise calls fn and returns the result.
This function is useful for providing a default result that is expensive to compute or has side effects.
Converts the Result<T, E> into a Promise<T>; useful for interacting with async code.
const result: Result<number, Error> = Ok(1);
const promise = result.toPromise(); // Promise<1>
const result2: Result<number, Error> = Err(new Error("error"));
const promise2 = result2.toPromise(); // Promise<never>
try {
const value = await result2.toPromise();
} catch (e) {
console.log(e); // "error"
}
Returns the success value. If the result is Err, this will throw an error with the provided message.
Returns the error value. If the result is Ok, this will throw an error with the provided message.
Returns the success value. If the result is Err, this will throw an error.
Returns the error value. If the result is Ok, this will throw an error.
Returns the success value. If the result is Err, this will return the provided default value.
Returns the success value. If the result is Err, this will call the provided function and return its result.
This function is useful for providing a default value that is expensive to compute or has side effects.
All the methods available on
Result<T, E>.