Files
lodash/attempt.js
John-David Dalton ce259221bd Bump to v4.4.0.
2016-02-15 20:20:54 -08:00

36 lines
962 B
JavaScript

define(['./_apply', './isError', './rest'], function(apply, isError, rest) {
/** Used as a safe reference for `undefined` in pre-ES5 environments. */
var undefined;
/**
* Attempts to invoke `func`, returning either the result or the caught error
* object. Any additional arguments are provided to `func` when it's invoked.
*
* @static
* @memberOf _
* @category Util
* @param {Function} func The function to attempt.
* @returns {*} Returns the `func` result or error object.
* @example
*
* // Avoid throwing errors for invalid selectors.
* var elements = _.attempt(function(selector) {
* return document.querySelectorAll(selector);
* }, '>_>');
*
* if (_.isError(elements)) {
* elements = [];
* }
*/
var attempt = rest(function(func, args) {
try {
return apply(func, undefined, args);
} catch (e) {
return isError(e) ? e : new Error(e);
}
});
return attempt;
});