Function attempt

  • Attempts to invoke func, returning either the result or the caught error object. Any additional arguments are provided to func when it's invoked.

    Type Parameters

    • T extends ((...args) => any)

    Parameters

    • func: T

      The function to attempt.

    • Rest ...args: any[]

      The arguments to invoke func with.

      Rest

    Returns ReturnType<T> | Error

    Returns the func result or error object.

    Since

    5.3.0

    Example

    // Avoid throwing errors for invalid selectors.
    const elements = attempt(selector =>
    document.querySelectorAll(selector), '>_>')

    if (isError(elements)) {
    elements = []
    }