Options
All
  • Public
  • Public/Protected
  • All
Menu

Module attempt

Index

References

Functions Util

References

Renames and re-exports attempt

Util Functions

  • attempt<T>(func: T, ...args: any[]): ReturnType<T> | Error
  • Attempts to invoke func, returning either the result or the caught error object. Any additional arguments are provided to func when it's invoked.

    since

    5.3.0

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

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

    Type Parameters

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

    Parameters

    • func: T

      The function to attempt.

    • Rest ...args: any[]

      The arguments to invoke func with.

    Returns ReturnType<T> | Error

    Returns the func result or error object.