import { Task } from "https://deno.land/x/eitherway@0.10.0/lib/async/mod.ts";
Use this to conditionally pass-through the encapsulated value <E>
based upon the outcome of the supplied ensureFn
.
In case of Ok<T>
, this method short-circuits.
In case of Err<E>
, the supplied ensureFn
is called with the encapsulated
value <E>
and if the return value is:
Ok<T2>
: it is returnedErr<T2>
: it is discarded and the originalErr<E>
is returned
See Task#andEnsure
for the opposite case.
This is equivalent to chaining:
original.orElse(ensureFn).or(original)
LHS orEnsure RHS | RHS: Ok | RHS: Err |
---|---|---|
LHS: Ok | Ok | Ok |
LHS: Err | Ok | Err |
Examples
Example 1
Example 1
import { Task } from "./task.ts";
declare function getConfig(): Task<string, RangeError>;
declare function getFallback(err: RangeError): Task<string, Error>;
declare function configureService(path: string): Task<void, TypeError>;
getConfig()
.orEnsure(getFallback)
.andThen(configureService)
.inspect((_: void) => console.log("Done!"))
.inspectErr((err: RangeError | TypeError) => console.log(err))