Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Result<E, A>

A Result represents a computation that may succeed or fail. Ok represents a successful computation, while Err represents a failure.

Type parameters

  • E

  • A

Hierarchy

Index

Methods

Abstract andThen

  • andThen<B>(fn: function): Result<E, B>
  • Chains together two computations that return results. If the result is a success, then the second computation is run. Otherwise, the Err is returned.

    Type parameters

    • B

    Parameters

    • fn: function

    Returns Result<E, B>

Abstract ap

  • Apply the value of a successful result to a function result. If this result is an Err, nothing happens. If this result is NOT a function, a TypeError is raised.

    Type parameters

    • B

    • C

    Parameters

    Returns Result<E, C>

Abstract assign

  • assign<K, B>(k: K, other: Result<E, B> | function): Result<E, A & object>
  • Encapsulates a common pattern of needing to build up an Object from a series of Result values. This is often solved by nesting andThen calls and then completing the chain with a call to ok.

    This feature was inspired (and the code lifted from) this article: https://medium.com/@dhruvrajvanshi/simulating-haskells-do-notation-in-typescript-e48a9501751c

    Wrapped values are converted to an Object using the Object constructor before assigning. Primitives won't fail at runtime, but results may be unexpected.

    Type parameters

    • K: string

    • B

    Parameters

    • k: K
    • other: Result<E, B> | function

    Returns Result<E, A & object>

Abstract cata

  • Folds over types; a switch/case for success or failure.

    Type parameters

    • B

    Parameters

    Returns B

Abstract do

  • do(fn: function): Result<E, A>
  • Inject a side-effectual operation into a chain of Result computations.

    The primary use case for do is to perform logging in the middle of a flow of Results.

    The side effect only runs when there isn't an error (Ok).

    The value will (should) remain unchanged during the do operation.

    ok({}) .assign('foo', ok(42)) .assign('bar', ok('hello')) .do(scope => console.log('Scope: ', JSON.stringify(scope))) .map(doSomethingElse)

    Parameters

    • fn: function
        • (a: A): void
        • Parameters

          • a: A

          Returns void

    Returns Result<E, A>

Abstract getOrElse

  • getOrElse(fn: function): A
  • Returns the value from a successful result. For an error, returns the result of evaluating the fn

    Parameters

    • fn: function
        • (): A
        • Returns A

    Returns A

Abstract getOrElseValue

  • getOrElseValue(defaultValue: A): A
  • Returns the value from a successful result. Returns the defaultValue if the result was a failure.

    Parameters

    • defaultValue: A

    Returns A

Abstract map

  • map<B>(fn: function): Result<E, B>
  • Returns a new result after applying fn to the value stored in a successful result. If the result was a failure, then the Err result is simply returned.

    Type parameters

    • B

    Parameters

    • fn: function
        • (_: A): B
        • Parameters

          • _: A

          Returns B

    Returns Result<E, B>

Abstract mapError

  • mapError<X>(fn: function): Result<X, A>
  • Returns a new result after applying fn to the error value. successful results are returned unchanged.

    Type parameters

    • X

    Parameters

    • fn: function
        • (_: E): X
        • Parameters

          • _: E

          Returns X

    Returns Result<X, A>

Abstract orElse

  • orElse<X>(fn: function): Result<X, A>
  • Runs an alternative computation in the case that the first computation resulted in an Err.

    Type parameters

    • X

    Parameters

    • fn: function

    Returns Result<X, A>

Generated using TypeDoc