@cprussin
    Preparing search index...

    A type which represents values that may encode either a successful result or an error result.

    Type Parameters

    • T extends NonNullable<unknown>

      the type of successful results

    • E extends NonNullable<unknown> = Error

      the type of error results

    Index

    Boolean operators

    Constructing

    Extracting the contained value

    Interacting with Option

    Querying the variant

    Transforming contained values

    Boolean operators

    • Takes a function which takes the value contained in this and returns a new Result; calls it and returns the result if this is Ok, otherwise returns this. Also sometimes known in other languages or libraries as flatmap or bind.

      Type Parameters

      • U extends {}

        the type contained in the Result returned by fn

      Parameters

      • this: Result<T, E>
      • fn: (value: T) => Result<U, E>

        a function that will be called with the value in this if this is Ok

      Returns Result<U, E>

      the result of calling fn if this is Ok, this otherwise

    • Takes a function which takes the value contained in this and returns a promise which resolves to a new Result; calls it and returns the result if this is Ok, otherwise returns a promise which resolves to this. This is an async version of andThen.

      Type Parameters

      • U extends {}

        the type contained in the Result returned by fn

      Parameters

      • this: Result<T, E>
      • fn: (value: T) => Promise<Result<U, E>>

        a function that will be called with the value in this if this is Ok

      Returns Promise<Result<U, E>>

      the result of calling fn if this is Ok, this otherwise

    • Takes a function which takes an error and returns a Result; calls it on the contained error and returns the result if this is Err, otherwise returns this.

      Type Parameters

      • F extends {} = Error

      Parameters

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

        a function that will be called with the error value if this is Err

      Returns Result<T, F>

      the result of calling fn if this is Err, this otherwise

    • Takes a function which takes an error and returns a promise which resolves to a new Result; calls it with the contained error and returns the result if this is Err, otherwise returns a promise which resolves to this.

      Type Parameters

      • F extends {} = Error

      Parameters

      • this: Result<T, E>
      • fn: (error: E) => Promise<Result<T, F>>

        a function that will be called if this is Err

      Returns Promise<Result<T, F>>

      the result of calling fn if this is Err, this otherwise

    Constructing

    • Construct a Result containing an error value.

      Type Parameters

      • T extends {}

        the type of success values

      • E extends {} = Error

        the type of the error value

      Parameters

      • error: E

        the error contained by the Result

      Returns Result<T, E>

      a Result containing an error error

    • Construct a Result containing a success value.

      Type Parameters

      • T extends {}

        the type of the value

      • E extends {} = Error

        the type of error results

      Parameters

      • value: T

        the value contained by the Result

      Returns Result<T, E>

      a Result containing value

    • Takes a function that could be throw and converts it into an Result.

      Type Parameters

      • T extends {}

        the type contained by the Result

      Parameters

      • fn: () => T

        the function which could throw

      Returns Result<T, Option<{}>>

      Ok with the return value of fn if fn doesn't throw, otherwise Err containing an Option which is None if the exception is null or undefined, and is Some with the exception otherwise

    • Takes a promise that could reject and converts it into an Result.

      Type Parameters

      • T extends {}

        the type contained by the Result

      Parameters

      • promise: Promise<T>

        the promise to convert

      Returns Promise<Result<T, Option<{}>>>

      a Promise containing Ok with the value resolved by promise if promise doesn't reject, otherwise Err containing an Option which is None if promise rejects with a null or undefined, and is Some with the rejection value otherwise

    Extracting the contained value

    • Takes two functions, one is called with the contained value if this is Ok and the other is called with the error if this is Err.

      Type Parameters

      • U

        the type of the return value of the matcher functions

      Parameters

      • this: Result<T, E>
      • matchers: { Err: (error: E) => U; Ok: (value: T) => U }

        an object containing matcher functions

        • Err: (error: E) => U

          A function that will be called on the value contained if this is Err.

        • Ok: (value: T) => U

          A function that will be called on the value contained if this is Ok.

      Returns U

      the return value of the Ok or Err matcher function

    • Returns the contained value if this is Ok, otherwise return the provided default value, ignoring any error value.

      Parameters

      • this: Result<T, E>
      • defaultValue: T

        the value to return if this is Err

      Returns T

      the contained if this is Ok, otherwise defaultValue

    • Returns the contained value if this is Ok, otherwise call the provided function with the error value and return the result.

      Parameters

      • this: Result<T, E>
      • defaultValue: (error: E) => T

        a function that will be called on the error value if this is Err

      Returns T

      the contained if this is Ok, otherwise the return value from calling defaultValue on the error value

    Interacting with Option

    Querying the variant

    • Check if this is Err.

      Parameters

      Returns boolean

      true if this is Err, false otherwise

      isOk

    Transforming contained values

    • Transforms Result<T, E> to Result<U, E> by applying the provided function to the contained value of Ok and leaving Err values unchanged.

      Type Parameters

      • U extends {}

        the type of the return value of fn

      Parameters

      • this: Result<T, E>
      • fn: (value: T) => U

        the function to apply to the value contained if this is Ok

      Returns Result<U, E>

      this if this is Err, otherwise Ok containing the result of applying fn to the value in this

    • Transforms Result<T, E> to Promise<Result<U, E>> by applying the provided async function to the contained value of Ok and resolving Err values unchanged.

      Type Parameters

      • U extends {}

        the type of the value in the promise returned by fn

      Parameters

      • this: Result<T, E>
      • fn: (value: T) => Promise<U>

        the function to apply to the value contained if this is Ok

      Returns Promise<Result<U, E>>

      a promise resolving to this if this is Err, otherwise a promise resolving to Ok containing the value resolved by the promise returned from applying fn to the value in this

    • Transforms Result<T, E> to Result<T, F> by applying the provided function to the contained value of Err and leaving Ok values unchanged.

      Type Parameters

      • F extends {} = Error

        the type of the return value of fn

      Parameters

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

        the function to apply to the value contained if this is Err

      Returns Result<T, F>

      this if this is Ok, otherwise Err containing the result of applying fn to the value in this

    • Transforms Result<T, E> to Promise<Result<T, F>> by applying the provided async function to the contained value of Err and resolving Ok values unchanged.

      Type Parameters

      • F extends {} = Error

        the type of the value in the promise returned by fn

      Parameters

      • this: Result<T, E>
      • fn: (error: E) => Promise<F>

        the function to apply to the value contained if this is Err

      Returns Promise<Result<T, F>>

      a promise resolving to this if this is Ok, otherwise a promise resolving to Err containing the value resolved by the promise returned from applying fn to the value in this

    • Applies the provided function to the contained value if this is Ok, otherwise returns the provided default value.

      Type Parameters

      • U extends {}

        the type of defaultValue and the value returned by fn

      Parameters

      • this: Result<T, E>
      • defaultValue: U

        the value to return if this is Err

      • fn: (value: T) => U

        the function to apply to the value contained if this is Ok

      Returns U

      the result of applying fn to the value in this if this is Ok, otherwise defaultValue

    • Takes an Array of Result values. If any of the Result values in the array is Err, returns that Err. Otherwise, returns Ok containing an Array of each value inside each Result in the original list.

      Type Parameters

      • T extends {}

        the type contained by the Result values

      • E extends {} = Error

        the error type of the Result values

      Parameters

      Returns Result<T[], E>

      Ok containing an Array of values contained by each Result in the original Array if all Result values in the original list are Ok, otherwise returns the first Err in the Array