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

    Parameters

    • func: ((...args: any[]) => T)

      The function to attempt.

        • (...args): T
        • Parameters

          • Rest...args: any[]

          Returns T

    • Optional Rest...args: any[]

      The arguments to invoke func with.

    Returns T | Error

    Returns the func result or error object.

    3.0.0

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

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