4.17.11
3.10.1
2.4.2
1.3.1

_.attempt(func, [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.

Since

3.0.0

Arguments

argument
func
[args]
type
Function
...*
description
The function to attempt.
The arguments to invoke `func` with.

Returns

(array)

Example

// Avoid throwing errors for invalid selectors.
var elements = _.attempt(function (
  selector
) {
  return document.querySelectorAll(
    selector
  );
},
">_>");

if (_.isError(elements)) {
  elements = [];
}
// Avoid throwing errors for invalid selectors.
var elements = _.attempt(function (selector) {
  return document.querySelectorAll(selector);
}, ">_>");

if (_.isError(elements)) {
  elements = [];
}