Constructs an AsyncResult
from a Result
or a Promise
of a Result
.
const ar = new AsyncResult(Promise.resolve('value'))
Calls mapper
if the result is Ok
, otherwise keeps the Err
value intact.
This function can be used for control flow based on Result
values.
let goodResult = Ok(1).toAsyncResult()
let badResult = Err('boo').toAsyncResult()
await goodResult.andThen(async (value) => Ok(value * 2)).promise // Ok(2)
await goodResult.andThen(async (value) => Err(`${value} is bad`)).promise // Err('1 is bad')
await badResult.andThen(async (value) => Ok(value * 2)).promise // Err('boo')
Maps an AsyncResult<T, E>
to AsyncResult<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.
let goodResult = Ok(1).toAsyncResult()
let badResult = Err('boo').toAsyncResult()
await goodResult.map(async (value) => value * 2).promise // Ok(2)
await badResult.andThen(async (value) => value * 2).promise // Err('boo')
A promise that resolves to a synchronous result.
Await it to convert AsyncResult<T, E>
to Result<T, E>
.
Private
then
An async-aware
Result
counterpart.Can be combined with asynchronous code without having to
await
anything right until the moment when you're ready to extract the finalResult
out of it.Can also be combined with synchronous code for convenience.