Function attempt

  • Wrapper for try/catch that tries to run a function and silences the throws by returning undefined instead.


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

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


    Function wrapped in try/catch.

    Type Parameters

    • Arguments extends readonly unknown[]

    • Output


    • wrappedFunction: GenericFunction<Arguments, Output>

      Function to be wrapped.

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

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

        • Rest ...parameters: Arguments

        Returns Maybe<Output>