Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Result<T>

A Result can be used to store the result of an operation. The operation can either be successful or considered as a failure. This class has been proposed by Khalil Stemmler.

Results are immutable

specfield

isFailure (boolean): determines whether or not the operation has failed

specfield

isSuccess (boolean): determines whether or not the operation is successful

specfield

error (Error) : contains the error causing the failure (if the operation failed)

specfield

value (T) : contains the result of the operation (if it is successful)

Type parameters

  • T

    Return type if the result is successful

Hierarchy

  • Result

Index

Constructors

Properties

Methods

Constructors

Private constructor

  • new Result(isSuccess: boolean, error?: Error, value?: T): Result
  • Creates a new Result<T> so that:

    • this_post.isSuccess = isSuccess &&
    • this_post.isFailure = !isSuccess &&
    • this_post.error = error if isSuccess = = false &&
    • this_post.value = value if isSuccess = = true

    Parameters

    • isSuccess: boolean

      Determines whether or not the operation is a success

    • Optional error: Error

      The error to be held in case the operation failed

    • Optional value: T

      The resulting value if the operation was successful

    Returns Result

Properties

Private _value

_value: T

error

error: Error

isFailure

isFailure: boolean

isSuccess

isSuccess: boolean

Methods

getValue

  • getValue(): T
  • throws

    A new Error if this.isFailure = = true

    willreturn

    Otherwise, returns this.value

    Returns T

Static combine

  • willreturn

    A new Result rso that r.isFailure is true if any of the results held in results is a failure

    Parameters

    • results: Result<any>[]

      Set of results to combine to get an overall result

    Returns Result<any>

Static fail

  • fail<U>(error: Error): Result<U>
  • willreturn

    A new Result r considered as a failure and so that r.error = error

    Type parameters

    • U

      Type of Result to be held as value

    Parameters

    • error: Error

    Returns Result<U>

Static ok

  • willreturn

    A new Result r considered as successful and so that r.value = value

    Type parameters

    • U

      Type of Result to be held as value

    Parameters

    • Optional value: U

      The value to be returned if the operation is successful

    Returns Result<U>

Generated using TypeDoc