Home

function: attempt


attempt(func: function, args: *): *

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

Example(s):

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

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

Params:

Name Type Attribute Description
func The function to attempt.
args The arguments to invoke `func` with.

Returns:

*

  {
    "comment": "/**\r\n * Attempts to invoke `func`, returning either the result or the caught error\r\n * object. Any additional arguments are provided to `func` when it's invoked.\r\n *\r\n * @since 3.0.0\r\n * @category Util\r\n * @param {Function} func The function to attempt.\r\n * @param {...*} [args] The arguments to invoke `func` with.\r\n * @returns {*} Returns the `func` result or error object.\r\n * @example\r\n *\r\n * // Avoid throwing errors for invalid selectors.\r\n * const elements = attempt(selector =>\r\n *   document.querySelectorAll(selector), '>_>')\r\n *\r\n * if (isError(elements)) {\r\n *   elements = []\r\n * }\r\n */",
    "meta": {
        "range": [
            639,
            788
        ],
        "filename": "attempt.js",
        "lineno": 22,
        "path": "C:\\Users\\beaujeup\\projects\\jsdoc-template\\examples\\lodash\\lodash-repo",
        "code": {
            "id": "astnode100000150",
            "name": "attempt",
            "type": "FunctionDeclaration",
            "paramnames": [
                "func",
                "args"
            ]
        }
    },
    "description": "Attempts to invoke `func`, returning either the result or the caught error\robject. Any additional arguments are provided to `func` when it's invoked.",
    "since": "3.0.0",
    "tags": [
        {
            "originalTitle": "category",
            "title": "category",
            "text": "Util",
            "value": "Util"
        }
    ],
    "params": [
        {
            "type": {
                "names": [
                    "function"
                ]
            },
            "description": "The function to attempt.",
            "name": "func"
        },
        {
            "type": {
                "names": [
                    "*"
                ]
            },
            "optional": true,
            "variable": true,
            "description": "The arguments to invoke `func` with.",
            "name": "args"
        }
    ],
    "returns": [
        {
            "type": {
                "names": [
                    "*"
                ]
            },
            "description": "Returns the `func` result or error object."
        }
    ],
    "examples": [
        "// Avoid throwing errors for invalid selectors.\rconst elements = attempt(selector =>\r  document.querySelectorAll(selector), '>_>')\r\rif (isError(elements)) {\r  elements = []\r}"
    ],
    "name": "attempt",
    "longname": "attempt",
    "kind": "function",
    "scope": "global",
    "___id": "T000002R000014",
    "___s": true,
    "filepath": "attempt.js"
}