Options
All
  • Public
  • Public/Protected
  • All
Menu

Class ExtendedTry<T>

Exports the Try type, but augmented with the unary property.

Type parameters

  • T

Hierarchy

  • Try<T>
    • ExtendedTry

Implements

Index

Properties

Static unary

unary: Unary = unary

A convenience property providing access to the unary operators.

Methods

Abstract catch

  • catch(consumer: (e: Error) => void): Try<T>
  • Used for performing side effects on failures: the given function will be invoked if and only if this Try is a failure, in which case it will be invoked with the encapsualted error.

    Note that any errors thrown by the consumer will not be handled.

    throw

    Anything thrown by consumer.

    see

    Try.forEach For a similar mechanism, but for successes and their values.

    see

    Try.tap

    see

    Try.recover If you want map to a new Try in the event of an error.

    Parameters

    • consumer: (e: Error) => void

      A function that will consume the error, if there is one.

        • (e: Error): void
        • Parameters

          • e: Error

          Returns void

    Returns Try<T>

    returns the same Try.

Abstract failed

  • failed(): Try<Error>
  • Turns a Failure into a Success and vice-versa. A Failure is turned into a Success encapsulating the error as it's value. A Success is turned into a new Failure.

    You can kind of think of this as an assertion that the try is a failure: so if it is, the assertion passes, so it results in a Success. If it's not a Failure, then the assertion fails, so it results in a Failure.

    Returns Try<Error>

Abstract filter

  • filter(predicate: (T: any) => boolean): Try<T>
  • If the Try is a success and its value passes the given predicate, the Try is returned. If it does not pass the predicate, or if the predicate throws, a Failure is returned. If the Try is already a Failure, it is returned.

    Parameters

    • predicate: (T: any) => boolean

      The predicate function to test this against.

        • (T: any): boolean
        • Parameters

          • T: any

          Returns boolean

    Returns Try<T>

Abstract flatMap

  • flatMap<U>(mapper: (t: T) => Try<U>): Try<U>
  • Like map, except the mapper itself returns a Try.

    Type parameters

    • U

    Parameters

    • mapper: (t: T) => Try<U>
        • (t: T): Try<U>
        • Parameters

          • t: T

          Returns Try<U>

    Returns Try<U>

Abstract forEach

  • forEach(consumer: (val: T) => void): Try<T>
  • A Try acts like a collection of 0 or 1 values, and this applies the given consumer to each item in the collection. This is used for performing side effects.

    Note that any errors thrown by the consumer will not be handled.

    throw

    Anything thrown by consumer.

    see

    Try.tap

    see

    Try.catch

    see

    Try.toArray

    Parameters

    • consumer: (val: T) => void

      A function that will consume the value, if there is one.

        • (val: T): void
        • Parameters

          • val: T

          Returns void

    Returns Try<T>

    returns the same Try.

Abstract get

  • get(): T
  • Gets the encapsulated value if it's successful, otherwise throws the encapsulated error.

    throws

    The encapsualted error if this is a Failure.

    Returns T

    the encapsulated value

Abstract getOrElse

  • getOrElse(defaultValue: T): T
  • Get the encpauslated value from a Success, or else return the given default value for a Failure.

    Parameters

    • defaultValue: T

      The default value to return if this is a Failure.

    Returns T

Abstract isFailure

  • isFailure(): boolean

Abstract isSuccess

  • isSuccess(): boolean

Abstract map

  • map<U>(mapper: (t: T) => U): Try<U>
  • Maps the encapsulated value of a success through the given mapper and returns a success encapsulating the result. If the mapper throws an error, it's encapsulated in a failure. If this try is already a failure, a new failure (of the appropriate type) encapsulating the same error is returned.

    Type parameters

    • U

    Parameters

    • mapper: (t: T) => U
        • (t: T): U
        • Parameters

          • t: T

          Returns U

    Returns Try<U>

Abstract orElse

  • orElse(defaultTryValue: Try<T>): Try<T>
  • Somewhat like getOrElse, but this doesn't do the get portion of it, meaning it doesn't give you the encapsulated value, it gives you a Try. If this is a success, returns itself. If this is a failure, returns the given value, as is.

    Parameters

    • defaultTryValue: Try<T>

      The encapsulated value (or failure) to use if this is a failure.

    Returns Try<T>

Abstract permissive

  • permissive(): Try<T | Error>
  • Returns a permissive Try which encapsulates both successes and failures as successes. For successes, returns a Try with the same encapsulated value. For failures, returns a success whose encapsulated value is the encapsulated error.

    Returns Try<T | Error>

Abstract recover

  • recover(errorMapper: (Error: any) => T): Try<T>
  • Recovers a Failure Try by mapping the encapsulated error into a valid value with the given mapper. If the given mapper throws an error, it's returned wrapped inside a new Failure. If this Try is a Success, it is returned unchanged.

    Parameters

    • errorMapper: (Error: any) => T

      Function that turns an error into a value, or throws an error if the given error is not recoverable.

        • (Error: any): T
        • Parameters

          • Error: any

          Returns T

    Returns Try<T>

Abstract recoverWith

  • recoverWith(recoverer: (Error: any) => Try<T>): Try<T>
  • Possibly recovers a Failure Try by mapping the encapsulated error into a Try. This is similar to recover, but the error mapper's returned value is assumed to already be a Try, which is returned. If the mapper throws an error, it's returned in a new Failure. If this Try is already a Success, it is returned as is.

    Parameters

    • recoverer: (Error: any) => Try<T>

      Function that turns an error into a Try, with a failure for unreocverable errors (or errors that occurreed attemptig to recover), or with a success encapsulating a recovered value/

        • (Error: any): Try<T>
        • Parameters

          • Error: any

          Returns Try<T>

    Returns Try<T>

Abstract safeTransform

  • safeTransform<U>(mapSuccess: (T: any) => Try<U>, mapFailure: (Error: any) => Try<U>): Try<U>
  • Similar to transform, except that any error thrown by the selected mapper function is captured and returned as a Failure.

    Type parameters

    • U

    Parameters

    • mapSuccess: (T: any) => Try<U>

      The function applied to the encapsulated value of a success, to get the new Try.

        • (T: any): Try<U>
        • Parameters

          • T: any

          Returns Try<U>

    • mapFailure: (Error: any) => Try<U>

      The function applied to the encapsulated error of a failure, to get the new Try.

        • (Error: any): Try<U>
        • Parameters

          • Error: any

          Returns Try<U>

    Returns Try<U>

Abstract tap

  • tap(valueConsumer: (val: T) => void, errorConsumer: (e: Error) => void): Try<T>
  • Used to perform side effects on success or failure.

    Note that any errors thrown by the selected consumer will not be handled.

    throw

    Anything thrown by the invoked consuerm.

    see

    Try.forEach

    see

    Try.catch

    see

    Try.transform

    Parameters

    • valueConsumer: (val: T) => void

      The function that will be called to consume the encapsulated value if this Try is a success.

        • (val: T): void
        • Parameters

          • val: T

          Returns void

    • errorConsumer: (e: Error) => void

      The function that will be called to consume the encapsulated error if this Try is a failure.

        • (e: Error): void
        • Parameters

          • e: Error

          Returns void

    Returns Try<T>

    return this same Try.

Abstract toArray

  • toArray(): Array<T>
  • Returns a new array containing the encapsulated value for a Success, or no values (an empty array) for a Failure.

    Returns Array<T>

Abstract toHungObservable

  • Converts this Try to an Observable stream that works the same as a supressed observable stream returned by toSuppressingObservable, except the stream never completes (for either the failure or success case).

    Type parameters

    Parameters

    Returns O

Abstract toMaybe

  • Converts this to a Maybe using the provided factory. A success is converted to a Maybe of the encapsulated value using the provided Just function. A failure returns the Nothing value.

    Type parameters

    Parameters

    • Maybe: MaybeFactory<M, T>

      An object that provides the Just factory function for creating a Maybe, and the Nothing singleton Maybe instance.

    Returns M

Abstract toNullable

  • toNullable(): T | null
  • Returns the encapsulated value of a success, or null for a failure. Note that the encapsulated value of a success may be a null.

    Returns T | null

Abstract toObservable

  • Converts this Try to an Observable stream: A success returns an Observable that emits the encapsulated value and then completes, a failure turns an Observable that err's.

    Type parameters

    Parameters

    • Observable: ObservableFactory<O, T>

      a factory function that is called to create the returned observable by providing it with the "subscribe" function. Note that this is not called with new, so if your Observable is a constructor, you'll need to encapsulate it in a factory function.

    Returns O

Abstract toOption

  • Converts this to an Option using the provided factory object. A success is converted to an Option of the encapsulated value, a failure is converted to a None.

    see

    Try.toOptional

    Type parameters

    Parameters

    • Option: OptionFactory<O, T>

      An object that provides the Some and None factory function for creating an Option.

    Returns O

Abstract toOptional

  • Converts this to an Optional, as long as you can provide it with an appropriate factory. A success is returned as an Optional of the encapsulated value, a failure is returned as an empty.

    see

    Try.toOption

    Type parameters

    Parameters

    • Optional: OptionalFactory<O, T>

      an object that provides the of and empty factory functions for creating an Optional (denoted by type parameter O).

    Returns O

Abstract toPromise

  • toPromise(): Promise<T>
  • Converts to this a settled promise. A success is converted to a promise that fulfills with the encapsulated value, a failure is converted to a promise that rejects with the encapsulated error.

    Returns Promise<T>

Abstract toSuppressingObservable

  • Converts this Try to an Observable stream that supresses the encapsulated error of a failure. Same was toObservable, but the failure case just completes immediately.

    Type parameters

    Parameters

    Returns O

Abstract transform

  • transform<U>(mapSuccess: (T: any) => Try<U>, mapFailure: (Error: any) => Try<U>): Try<U>
  • Transforms this Try into another Try by transforming the encapsulated value of a success, or the encapsulated error of a failure through the given functions.

    Note: if the applied function throws an error, it is not captured, it is thrown. If you want to capture it in a Failure, use safeTransform instead.

    throws

    Anything thrown by the applied mapper funtion.

    see

    Try.safeTransform

    Type parameters

    • U

    Parameters

    • mapSuccess: (T: any) => Try<U>

      The function applied to the encapsulated value of a success, to get the new Try.

        • (T: any): Try<U>
        • Parameters

          • T: any

          Returns Try<U>

    • mapFailure: (Error: any) => Try<U>

      The function applied to the encapsulated error of a failure, to get the new Try.

        • (Error: any): Try<U>
        • Parameters

          • Error: any

          Returns Try<U>

    Returns Try<U>

Abstract transmute

  • transmute<U>(mapSuccess: (T: any) => U, mapFailure: (Error: any) => U): U
  • Unpacks the Try into a value by applying one function for successes, and one for failures. Similar to transform except the mappers aren't assumed to return a Try.

    throws

    Any error thrown by the selected mapper function.

    Type parameters

    • U

    Parameters

    • mapSuccess: (T: any) => U
        • (T: any): U
        • Parameters

          • T: any

          Returns U

    • mapFailure: (Error: any) => U
        • (Error: any): U
        • Parameters

          • Error: any

          Returns U

    Returns U

Static Failure

  • Failure<T>(error: Error): Try<T>
  • Not recommended, you should generally prefer using Try.apply instead of instantiating a new Success or Failure directly.

    Type parameters

    • T

    Parameters

    • error: Error

    Returns Try<T>

Static Success

  • Success<T>(value: T): Try<T>
  • Not recommended, you should generally prefer using Try.apply instead of instantiating a new Success or Failure directly.

    Type parameters

    • T

    Parameters

    • value: T

    Returns Try<T>

Static apply

  • apply<T>(provider: () => T): Try<T>
  • Execute the given function and encapsulate the result in a Try, whether successful or not. If the func returns a value, this is returned encapsulated in a Success. If the func throws an error, it is captured in a Failure and returned.

    Type parameters

    • T

    Parameters

    • provider: () => T

      The function to invoke.

        • (): T
        • Returns T

    Returns Try<T>

Static createTryOperator

  • Creates an operator for Observables in the style of rxjs. The operator will map an observable to one that emits tries: values will be mapped to successes encapsulating those values, and an error will be mapped to a failure encapsulating that error. The generated observable will terminate when the source observable terminate or errors.

    Type parameters

    Parameters

    • Observable: ObservableFactory<O2, Try<T>>

      The factory function for creating a new Observable from a subscribe function. Note that this is not called with new, so if your function is a costructor, you'll need to encapsulate that in another function. For instance, for rxjs, this could be (subscribe) => new rxjs.Observable(subscribe). The subscribe function that will be passed to Observable is expected to be called with an Observer object that has next, error, and complete methods.

    Returns (source: O) => O2

    The operator function which will take a source Observable and return a derived Observable that emits Tries as described above. The source Observable passed to the returned function is expected to have a subscribe method which takes three arguments: onNext, onError, and onComplete.

      • (source: O): O2
      • Parameters

        • source: O

        Returns O2

Static createUnTryOperator

  • createUnTryOperator<O, O2, T>(Observable: ObservableFactory<O, T>): (source: O2) => O
  • Similar to createTryOperator, this returns an Observable operator that unpacks Try values emitted by an observable. Encapsulated values of successes are emitted as values, and a failure emitted by the source stream is unpacked and its encapsulated error is put out as an error.

    Type parameters

    Parameters

    • Observable: ObservableFactory<O, T>

      The factory function for creating a new Observable from a subscribe function. Now that this is not called with new, so if your function is costructor, you'll need to encapsulate that in a function. For instance, for rxjs, this could be (subscribe) => new rxjs.Observable(subscribe). The subscribe function that will be passed to Observable is expected to be called with an Observer object that has next, error, and complete methods.

    Returns (source: O2) => O

    The operator function which will take a source Observable and return a derived Observable that emits Tries as described above. The source Observable passed to the returned function is expected to have a subscribe method which takes three arguments: onNext, onError, and onComplete.

      • (source: O2): O
      • Parameters

        • source: O2

        Returns O

Static flatApply

  • flatApply<T>(trySupplier: () => Try<T>): Try<T>
  • Similar to apply, this executes a function and captures any exceptions thrown into a Failure. The difference from apply is that the given function is assumed to already return a Try, which is not wrapped in another Try, but returned as is.

    Type parameters

    • T

    Parameters

    • trySupplier: () => Try<T>

    Returns Try<T>

Static from

Static fromMaybe

  • Converts a Maybe to a Try. Assumes the Maybe implements the map and getOrElse methods; the former returns another Maybe with the encapsulated value (if any) transformed according to the given function; the latter returns the encapsulated value or invokes the provided supplier if the Maybe has no encapsulated value and returns the result.

    Type parameters

    • T

    Parameters

    • maybe: Maybe<T>

      A Maybe object

    Returns Try<T>

Static fromOption

  • Convert a scala-like Option object to a Try. If the option is defined (as defined by it's isDefined method returning a truthy value), it's value (as returned by it's get method) is returned encapsulated in a success. Otherwise a Failure is returned.

    Note that error thrown attempting to invoke the method of the option are not handled, they will be thrown. The get method is only invoked if isDefined returns a truthy value.

    see

    Try.fromOptional

    Type parameters

    • T

    Parameters

    • option: Option<T>

      An Option object.

    Returns Try<T>

Static fromOptional

  • Convert a java-like Optional object to a Try. If the optional is present (as defined by it's isPresent method returning a truthy value), it's value (as returned by it's get method) is returned encapsulated in a success. Otherwise a Failure is returned.

    Note that error thrown attempting to invoke the method of the option are not handled, they will be thrown. The get method is only invoked if isPresent returns a truthy value.

    see

    Try.fromOption

    Type parameters

    • T

    Parameters

    • optional: Optional<T>

      An Optional object.

    Returns Try<T>

Static fromPromise

  • fromPromise<T>(p: Promise<T>): Promise<Try<T>>
  • Given a Promise for a value, returns a Promise for a Try of that value. The returned promise will always fulfill: if the given promise fulfills, the returned promise will fulfill with a success encapsulating the fulfillment value; if the given promise rejects, the returned promise will fulfill with a failure Try encapsulating the rejection error.

    Type parameters

    • T

    Parameters

    • p: Promise<T>

      The promise to convert to a Try.

    Returns Promise<Try<T>>

Generated using TypeDoc