Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Result<T, F>

Type parameters

  • T

  • F

Hierarchy

  • Result

Index

Constructors

Private constructor

Properties

Private Readonly wrapped

wrapped: Ok<T> | Failure<F>

Methods

apply

  • apply(fn: (wrapped: T) => void): Result<T, F>
  • Apply the provided callback function for successful result.

    Parameters

    • fn: (wrapped: T) => void

      Callback function

        • (wrapped: T): void
        • Parameters

          • wrapped: T

          Returns void

    Returns Result<T, F>

applyError

  • applyError(fn: (wrapped: F) => void): Result<T, F>
  • Apply the provided callback function for unsuccessful result.

    Parameters

    • fn: (wrapped: F) => void

      Callback function

        • (wrapped: F): void
        • Parameters

          • wrapped: F

          Returns void

    Returns Result<T, F>

flatMap

  • flatMap<U>(fn: (wrapped: T) => Result<U, F>): Result<U, F>
  • Transform the current Result<T, F> to a new Result<U, F>.

    typeparam

    Type parameters

    • U

    Parameters

    • fn: (wrapped: T) => Result<U, F>

      Transforming the wrapped value to a new Result

        • Parameters

          • wrapped: T

          Returns Result<U, F>

    Returns Result<U, F>

flatMapError

  • flatMapError<E>(fn: (wrapped: F) => Result<T, E>): Result<T, E>
  • Transform the current Result<T, F> to a new Result<T, E>.

    typeparam

    Type parameters

    • E

    Parameters

    • fn: (wrapped: F) => Result<T, E>

      Transforming the wrapped value to a new Result

        • Parameters

          • wrapped: F

          Returns Result<T, E>

    Returns Result<T, E>

get

  • get(): T
  • Get the wrapped value in the Result.

    try {
     const value = result.get();
    } catch (e) {
     // handle Failure
    }
    
    throws

    F

    Returns T

    the wrapped value

map

  • map<U>(fn: (wrapped: T) => U): Result<U, F>
  • Transform the wrapped "ok" value to a new value.

    typeparam

    the type of the new transformed value

    Type parameters

    • U

    Parameters

    • fn: (wrapped: T) => U

      Transforming the wrapped value

        • (wrapped: T): U
        • Parameters

          • wrapped: T

          Returns U

    Returns Result<U, F>

mapError

  • mapError<E>(fn: (wrapped: F) => E): Result<T, E>
  • Transform the wrapped "err" to a new error.

    typeparam

    Type parameters

    • E

    Parameters

    • fn: (wrapped: F) => E

      Transforming the wrapped error

        • (wrapped: F): E
        • Parameters

          • wrapped: F

          Returns E

    Returns Result<T, E>

match

  • match(cases: { err: (failure: F) => void; ok: (value: T) => void }): Result<T, F>
  • Pattern match on successful case or failure case.

    Parameters

    • cases: { err: (failure: F) => void; ok: (value: T) => void }

      Object containing callback function for when result is ok or err

      • err: (failure: F) => void
          • (failure: F): void
          • Parameters

            • failure: F

            Returns void

      • ok: (value: T) => void
          • (value: T): void
          • Parameters

            • value: T

            Returns void

    Returns Result<T, F>

Static err

  • err<T, F>(failure: F): Result<T, F>
  • Create an Result from a failure value.

    Type parameters

    • T

    • F

    Parameters

    • failure: F

    Returns Result<T, F>

Static of

  • of<T, F>(throwableFn: () => T | never): Result<T, F>
  • Transform a function to Result.

    Result.of(() => {
     return throwableFn();
    });
    

    Type parameters

    • T

    • F

    Parameters

    • throwableFn: () => T | never

      Throwable function which can throw or return an value

        • (): T | never
        • Returns T | never

    Returns Result<T, F>

Static ok

  • ok<T, F>(value: T): Result<T, F>
  • Create an Result from an "ok" value.

    Type parameters

    • T

    • F

    Parameters

    • value: T

    Returns Result<T, F>

Generated using TypeDoc