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