Calls mapper
if the result is Ok
, otherwise returns the Err
value of self.
This function can be used for control flow based on Result
values.
Returns the contained Ok
value, if exists. Throws an error if not.
The thrown error's
[cause'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause) is set to value contained in
Err`.
If you know you're dealing with Ok
and the compiler knows it too (because you tested
isOk()
or isErr()
) you should use value
instead. While Ok
's expect()
and value
will
both return the same value using value
is preferable because it makes it clear that
there won't be an exception thrown on access.
the message to throw if no Ok value.
Maps a Result<T, E>
to Result<U, E>
by applying a function to a contained Ok
value,
leaving an Err
value untouched.
This function can be used to compose the results of two functions.
Maps a Result<T, E>
to Result<U, E>
by either converting T
to U
using mapper
(in case of Ok
) or using the default_
value (in case of Err
).
If default
is a result of a function call consider using mapOrElse
instead, it will
only evaluate the function when needed.
Maps a Result<T, E>
to Result<U, E>
by either converting T
to U
using mapper
(in case of Ok
) or producing a default value using the default
function (in case of
Err
).
Returns Ok()
if we have a value, otherwise returns other
.
other
is evaluated eagerly. If other
is a result of a function
call try orElse()
instead – it evaluates the parameter lazily.
Ok(1).or(Ok(2)) // => Ok(1)
Err('error here').or(Ok(2)) // => Ok(2)
Returns Ok()
if we have a value, otherwise returns the result
of calling other()
.
other()
is called only when needed and is passed the error value in a parameter.
Ok(1).orElse(() => Ok(2)) // => Ok(1)
Err('error').orElse(() => Ok(2)) // => Ok(2)
Returns the contained Ok
value, but never throws.
Unlike unwrap()
, this method doesn't throw and is only callable on an Ok
Therefore, it can be used instead of unwrap()
as a maintainability safeguard
that will fail to compile if the error type of the Result is later changed to an error that can actually occur.
(this is the into_ok()
in rust)
Creates an AsyncResult
based on this Result
.
Useful when you need to compose results with asynchronous code.
Returns the contained Ok
value.
Because this function may throw, its use is generally discouraged.
Instead, prefer to handle the Err
case explicitly.
If you know you're dealing with Ok
and the compiler knows it too (because you tested
isOk()
or isErr()
) you should use value
instead. While Ok
's unwrap()
and value
will
both return the same value using value
is preferable because it makes it clear that
there won't be an exception thrown on access.
Throws if the value is an Err
, with a message provided by the Err
's value and
[`cause'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause)
set to the value.
Returns the contained Err
value.
Because this function may throw, its use is generally discouraged.
Instead, prefer to handle the Ok
case explicitly.
Throws if the value is an Ok
, with a message provided by the Ok
's value and
[`cause'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause)
set to the value.
Readonly
valueStatic
Readonly
EMPTY
Contains the success value