Class Try<TryResult>

A Try is a container for a value or an error. It is used to handle exceptions in a functional way. A Try can be successful or a failure. A successful Try contains a value, while a failed Try contains an error. A Try can be mapped to a new Try, or the value can be extracted from the Try. If the Try is a failure, the error can be extracted or mapped to a new error. The Try can also be used to perform actions on the value, or to provide a default value if the Try is a failure. The Try can be used to chain operations together.

Example

const result = Try.of(someFunctionThatMightThrow)
.map(value => value + 1)
.andThen(console.log)
.getOrElse(() => 5);

Type Parameters

  • TryResult

Constructors

Properties

error?: Error
isSuccessful: boolean
value?: TryResult

Methods

  • Perform an action on the embedded value, but continue with the same Try. Does not throw if the Try is a failure. If the transformer throws, the result will be a failed Try.

    Parameters

    • transformer: Transformer<TryResult, unknown>

      A function that takes the Try's value and returns nothing.

    Returns Try<TryResult>

    The original Try if it was a failure or the transformer succeeded, otherwise a new Try with the failure of the transformer.

  • Map the Try to a new Try, but flatten the result. If the Try is a failure, the new Try will be a failure.

    Type Parameters

    • NextTryResult

    Parameters

    Returns Try<NextTryResult>

    A new Try with the result of the transformer, a new Try with the error if one occured in the transformer, or the original Try if it is a failure.

  • Return the error embedded in the Try, or throw an error if the Try is a success.

    Returns Error

    The error embedded in the Try.

  • Return the value embedded in the Try, or throw the result of the executor if the Try is a failure.

    Parameters

    • failureMapper: ((error) => Error) = ...

      A function that takes the Try's error and returns an error.

        • (error): Error
        • Parameters

          • error: Error

          Returns Error

    Returns TryResult

    The value embedded in the Try.

  • Check if the Try is a failure.

    Returns boolean

    true if the Try is a failure, false otherwise.

  • Check if the Try is successful.

    Returns boolean

    true if the Try is successful, false otherwise.

  • Map the Try to a new AsyncTry with the result of the Async Transformer. If the Try is a failure, the new AsyncTry will be a failure.

    Type Parameters

    • NextTryResult

    Parameters

    • transformer: Transformer<TryResult, Promise<NextTryResult>>

      An async function that takes the Try's value and returns a new value as a Promise.

    Returns AsyncTry<NextTryResult>

    A new AsyncTry with the result of the transformer, a new AsyncTry with the error if one occured in the transformer, or a new AsyncTry with the original failure if this is a failure.

  • Map the embedded failure. Does nothing if the Try is a success.

    Parameters

    • failureMapper: ((error) => Error)

      A function that takes the Try's error and maps it to a new error.

        • (error): Error
        • Parameters

          • error: Error

          Returns Error

    Returns Try<TryResult>

    The original Try if it is a success, or a new Try with the mapped error.

  • Perform an action on the Error that caused this Try to be a failure. Does nothing if the Try is a success.

    Parameters

    • transformer: Transformer<Error, unknown>

      A function that takes the Try's error and returns nothing.

    Returns Try<TryResult>

    The original Try if it was a success or the transformer succeeded, otherwise a new Try with the failure of the transformer.

  • Map the embedded failure to a new successful Try. Does nothing if the Try is a success. If the failureMapper throws, the result will be a failed Try.

    Parameters

    • failureMapper: ((error) => TryResult)

      A function that takes the Try's error and maps it to a new successful Try.

    Returns Try<TryResult>

    The original Try if it is a success, or a new Try with the original failure mapped to a success.

  • Create a new failed Try with an error.

    Parameters

    • error: Error

      The error to embed in the Try.

    Returns Try<never>

    A new Try with the error.

  • Create a new Try by trying to execute the executor. If the executor throws an error, the Try will be a failure. If the executor returns a value, the Try will be successful and contain the return value of the executor.

    Type Parameters

    • TryResult

    Parameters

    • executor: Executor<TryResult>

      A function that might return a value or throw an error.

    Returns Try<TryResult>

    A new Try with the result of the executor, or a new Try with the error if one occured in the executor.

Generated using TypeDoc