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<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.
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)
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.
Private
Readonly
_stackReadonly
errorStatic
Readonly
EMPTYAn empty Err
Contains the error value