Files
lodash/utility/attempt.js
2015-03-06 01:11:05 -08:00

39 lines
927 B
JavaScript

import isError from '../lang/isError';
/**
* Attempts to invoke `func`, returning either the result or the caught error
* object. Any additional arguments are provided to `func` when it is invoked.
*
* @static
* @memberOf _
* @category Utility
* @param {*} 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 = [];
* }
*/
function attempt() {
var func = arguments[0],
length = arguments.length,
args = Array(length ? length - 1 : 0);
while (--length > 0) {
args[length - 1] = arguments[length];
}
try {
return func.apply(undefined, args);
} catch(e) {
return isError(e) ? e : new Error(e);
}
}
export default attempt;