Options
All
  • Public
  • Public/Protected
  • All
Menu

External module "failable"

Index

Type aliases

AsyncFunction

AsyncFunction: function

Helper type for an async function that takes Req and returns a FailablePromise<Res, Err>.

Type declaration

FailableArg

FailableArg: function

Type declaration

FailableArgParams

FailableArgParams: object

Type declaration

FailableAsyncArg

FailableAsyncArg: function

Type declaration

FailableAsyncFunctionParams

FailableAsyncFunctionParams: object

Type declaration

FailablePromise

FailablePromise: Promise<IFailable<T, E>>

IFailableResult

IFailableResult: object | object

Discriminated union for an IFailable result. value or error can be extracted from it using an if (r.isError) check

Variables

Const mapM

mapM: mapMultiple = mapMultiple

Functions

failable

  • Creates a failable comutation from a function. The supplied function receives an object containing helper functions to create IFailable values. You need to give generic arguments T and E to it indicating the success and failure types.

    example
    
    const computation1: () => Failable<string, string> = ...;
    const computation2: (x: string) => Failable<number, string> = ...;
    const computation3 = failable<number, string>(({ run, success, failure }) => {
      const str = run(computation1());
      const num = run(computation2(str));
      if (num > 10) {
        return success(num);
      } else {
        return failure("Number too small");
      }
    })
    

    Type parameters

    • T

    • E

    Parameters

    Returns IFailable<T, E>

failableAsync

  • Async version of failable that takes a computation that returns a Promise<Failable<T, E>>. It can be combined with async/await

    example
    
    const computation1: () => FailablePromise<string, string> = ...;
    const computation2: (x: string) => FailablePromise<number, string> = ...;
    const computation3: (x: number) => Failable<number, string> = ...;
    const computation4 = failableAsync<number, string>(async ({ run, success, failure }) => {
      const str = run(await computation1());
      const num1 = run(await computation2(str));
    
      // notice that computation3 returns a non async failable so await isn't required
      const num = run(computation3(num1));
      if (num > 10) {
        return success(num);
      } else {
        return failure("Number too small");
      }
    })
    

    Type parameters

    • T

    • E

    Parameters

    Returns Promise<IFailable<T, E>>

failure

  • Create an error IFailable value.

    Type parameters

    • T

    • E

    Parameters

    • err: E

      Error value

    Returns IFailable<T, E>

mapMultiple

  • mapMultiple<T, U, E>(arr: T[], f: function): IFailable<U[], E>
  • Take an array of elements and apply a failable computation to the array, one element at a time, returning an IFailable of items.

    Type parameters

    • T

    • U

    • E

    Parameters

    • arr: T[]

      Array of values of type T

    • f: function

      Function that takes an item of type T from the given array and returns an IFailable<U, E>.

    Returns IFailable<U[], E>

    A failable containing an array of U values wrapped inside an IFailable

success

  • Create a successful IFailable value

    Type parameters

    • T

    • E

    Parameters

    • value: T

      Result value

    Returns IFailable<T, E>

Object literals

Const Failable

Failable: object

Object containing static functions for IFailable. Anything that isn't an instance method should be added here.

failure

failure: failure

mapM

mapM: mapMultiple = mapMultiple

mapMultiple

mapMultiple: mapMultiple

of

of: success = success

success

success: success

Generated using TypeDoc