Function attempt

  • Wrapper for try/catch.

    Remarks

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

    Example

    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

    Parameters

    • 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>