• Runs a callback function and catches any errors that occur.

    If an error occurs, it will run the catchcallbackfn function, and return the result of that function, unless the finallycallbackfn function returns a value.

    It will then run the finallycallbackfn function, whether an error occurred or not, and return the result of that function or if it does not return anything, the result of the catchcallbackfn function, or if there was no error, the result of the trycallbackfn function.

    Type Parameters

    • TT extends unknown

      The type of the value returned by the trycallbackfn function.

    • CT extends unknown

      The type of the value returned by the catchcallbackfn function.

    • FT extends unknown

      The type of the value returned by the finallycallbackfn function.

    Parameters

    • trycallbackfn: () => TT

      The callback function to run.

    • Optionalcatchcallbackfn: (e: Error) => CT

      The callback function to run if an error occurs.

    • Optionalfinallycallbackfn: (v: TT | CT) => FT

      The callback function to run whether an error occurred or not.

    Returns TT | CT | FT

    The result of the finallycallbackfn function, unless it does not return a value, in which case it will return the result of the catchcallbackfn function, or if there was no error, the result of the trycallbackfn function.