FuncType - v1.4.1
    Preparing search index...

    Variable Try

    Try: <T>(f: () => T) => Try<T> & {
        async: <T>(thunk: () => Promise<T>) => Promise<Try<T>>;
        failure: <T>(error: string | Error) => Try<T>;
        fromBinary: <T>(binary: string) => Try<T>;
        fromJSON: <T>(json: string) => Try<T>;
        fromPromise: <T>(promise: Promise<T>) => Promise<Try<T>>;
        fromYAML: <T>(yaml: string) => Try<T>;
        isFailure: <T>(
            tryValue: Try<T>,
        ) => tryValue is Try<T> & { _tag: "Failure"; error: Error };
        isSuccess: <T>(
            tryValue: Try<T>,
        ) => tryValue is Try<T> & { _tag: "Success"; error: undefined };
        sequence: <T>(tries: Try<T>[]) => Try<T[]>;
        success: <T>(value: T) => Try<T>;
        traverse: <T, U>(
            arr: readonly T[],
            f: (value: T, index: number) => Try<U>,
        ) => Try<U[]>;
    }

    Type Declaration

      • <T>(f: () => T): Try<T>
      • Type Parameters

        • T

        Parameters

        • f: () => T

        Returns Try<T>

    • async: <T>(thunk: () => Promise<T>) => Promise<Try<T>>

      Creates a Try from a thunk that returns a Promise. The thunk is invoked when async() is called, so the Promise starts executing under the Try wrapper — synchronous throws from the thunk are caught the same way Try(() => sync) catches them, and rejections are caught the same way Try.fromPromise(promise) catches them.

      Prefer this over Try.fromPromise(thunk()) when you want the work to be deferred until wrapping (e.g. composing a chain of Try-returning thunks before any of them runs).

      const result = await Try.async(() => fs.readFile(path, "utf8"))
      result.fold(err => log(err.message), data => process(data))
    • failure: <T>(error: string | Error) => Try<T>

      Creates a Failure directly without needing to throw

    • fromBinary: <T>(binary: string) => Try<T>

      Creates a Try from binary string

    • fromJSON: <T>(json: string) => Try<T>

      Creates a Try from JSON string

    • fromPromise: <T>(promise: Promise<T>) => Promise<Try<T>>

      Creates a Try from a Promise, resolving to Success or Failure

    • fromYAML: <T>(yaml: string) => Try<T>

      Creates a Try from YAML string

    • isFailure: <T>(tryValue: Try<T>) => tryValue is Try<T> & { _tag: "Failure"; error: Error }

      Type guard to check if a Try is Failure

    • isSuccess: <T>(
          tryValue: Try<T>,
      ) => tryValue is Try<T> & { _tag: "Success"; error: undefined }

      Type guard to check if a Try is Success

    • sequence: <T>(tries: Try<T>[]) => Try<T[]>

      Combines an array of Trys into a single Try containing an array. Short-circuits on the first Failure, preserving its error.

    • success: <T>(value: T) => Try<T>

      Creates a Success directly without needing a callback

    • traverse: <T, U>(arr: readonly T[], f: (value: T, index: number) => Try<U>) => Try<U[]>

      Maps an array through a function returning Try, then sequences the results. Short-circuits on the first Failure.