Skip to main content
Using Deno in production at your company? Earn free Deno merch.
Give us feedback
Module

x/eitherway/lib/async/mod.ts>Task#andEnsure

Yet Another Option and Result Implementation - providing safe abstractions for fallible flows inspired by F# and Rust
Latest
method Task.prototype.andEnsure
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 original Ok<T> is returned
  • Err<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

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))

Parameters

ensureFn: (v: T) => Result<T2, E2> | PromiseLike<Result<T2, E2>>

Returns

Task<T, E | E2>