Function attempt

  • Wrapper for try/catch.


    This functions tries to run a function and silences the throws by wrapping it with a try/catch and returning undefined instead.


    const willFail = () => {
    throw new Error("fail");

    const safeWillFail = attempt(willFail);
    safeWillFail(); // undefined

    Type Parameters

    • Arguments extends readonly unknown[]

      Type of the arguments of the function to be wrapped.

    • Output


    • wrappedFunction: Function<Arguments, Output>

      Function to be wrapped.

    Returns ((...parameters) => Maybe<Output>)

    Function wrapped in try/catch.

      • (...parameters): Maybe<Output>
      • Parameters

        • Rest ...parameters: Arguments

        Returns Maybe<Output>