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#orEnsure

Yet Another Option and Result Implementation - providing safe abstractions for fallible flows inspired by F# and Rust
Latest
method Task.prototype.orEnsure
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 returned
  • Err<T2>: it is discarded and the original Err<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

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

Parameters

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

Returns

Task<T | T2, E>