Function attempt

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

    Example

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

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

    Returns

    Function wrapped in try/catch.

    Type Parameters

    • Arguments extends readonly unknown[]

    • Output

    Parameters

    • wrappedFunction: GenericFunction<Arguments, Output>

      Function to be wrapped.

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

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

        • Rest ...parameters: Arguments

        Returns Maybe<Output>