Function safelyTryOrElse

  • Given a function which takes no arguments and returns a Promise and a function which accepts an unknown rejection reason and transforms it into a known rejection type E, return a Task<T, E> for the result of invoking that function. This safely handles functions which fail synchronously or asynchronously, so unlike fromPromise is safe to use with values which may throw errors before producing a Promise.

    import { safelyTryOrElse } from 'true-myth/task';

    function throws(): Promise<number> {
    throw new Error("Uh oh!");
    }

    // Note: passing the function by name, *not* calling it.
    let theTask = safelyTryOr(
    (reason) => `Something went wrong: ${reason}`,
    throws
    );
    let theResult = await theTask;
    console.log(theResult.toString); // Err("Something went wrong: Error: Uh oh!")

    You can also write this in “curried” form, passing just the fallback value and getting back a function which accepts the:

    import { safelyTryOr } from 'true-myth/task';

    function throws(): Promise<number> {
    throw new Error("Uh oh!");
    }

    // Note: passing the function by name, *not* calling it.
    let withFallback = safelyTryOrElse<number, string>(
    (reason) => `Something went wrong: ${reason}`
    );
    let theResult = await withFallback(throws);
    console.log(theResult.toString); // Err("Something went wrong: Error: Uh oh!")

    Note that in the curried form, you must specify the expected T type of the resulting Task, or else it will always be unknown.

    Type Parameters

    • T
    • E

    Parameters

    • onError: (reason: unknown) => E

      The function to use to transform the rejectionr easons if the Promise produced by fn rejects.

    • fn: () => Promise<T>

      A function which returns a Promise when called.

    Returns Task<T, E>

    A Task which resolves to the resolution value of the promise or rejects with the rejection value of the promise or any error thrown while invoking fn.

  • Given a function which takes no arguments and returns a Promise and a function which accepts an unknown rejection reason and transforms it into a known rejection type E, return a Task<T, E> for the result of invoking that function. This safely handles functions which fail synchronously or asynchronously, so unlike fromPromise is safe to use with values which may throw errors before producing a Promise.

    import { safelyTryOrElse } from 'true-myth/task';

    function throws(): Promise<number> {
    throw new Error("Uh oh!");
    }

    // Note: passing the function by name, *not* calling it.
    let theTask = safelyTryOr(
    (reason) => `Something went wrong: ${reason}`,
    throws
    );
    let theResult = await theTask;
    console.log(theResult.toString); // Err("Something went wrong: Error: Uh oh!")

    You can also write this in “curried” form, passing just the fallback value and getting back a function which accepts the:

    import { safelyTryOr } from 'true-myth/task';

    function throws(): Promise<number> {
    throw new Error("Uh oh!");
    }

    // Note: passing the function by name, *not* calling it.
    let withFallback = safelyTryOrElse<number, string>(
    (reason) => `Something went wrong: ${reason}`
    );
    let theResult = await withFallback(throws);
    console.log(theResult.toString); // Err("Something went wrong: Error: Uh oh!")

    Note that in the curried form, you must specify the expected T type of the resulting Task, or else it will always be unknown.

    Type Parameters

    • T
    • E

    Parameters

    • onError: (reason: unknown) => E

      The function to use to transform the rejectionr easons if the Promise produced by fn rejects.

    Returns (fn: () => Promise<T>) => Task<T, E>

    A Task which resolves to the resolution value of the promise or rejects with the rejection value of the promise or any error thrown while invoking fn.