mirror of https://github.com/lukechilds/node.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
844 B
34 lines
844 B
9 years ago
|
var apply = require('./_apply'),
|
||
|
isObject = require('./isObject'),
|
||
|
rest = require('./rest');
|
||
10 years ago
|
|
||
|
/**
|
||
|
* Attempts to invoke `func`, returning either the result or the caught error
|
||
9 years ago
|
* object. Any additional arguments are provided to `func` when it's invoked.
|
||
10 years ago
|
*
|
||
|
* @static
|
||
|
* @memberOf _
|
||
9 years ago
|
* @category Util
|
||
10 years ago
|
* @param {Function} func The function to attempt.
|
||
|
* @returns {*} Returns the `func` result or error object.
|
||
|
* @example
|
||
|
*
|
||
9 years ago
|
* // Avoid throwing errors for invalid selectors.
|
||
10 years ago
|
* var elements = _.attempt(function(selector) {
|
||
|
* return document.querySelectorAll(selector);
|
||
|
* }, '>_>');
|
||
|
*
|
||
|
* if (_.isError(elements)) {
|
||
|
* elements = [];
|
||
|
* }
|
||
|
*/
|
||
9 years ago
|
var attempt = rest(function(func, args) {
|
||
10 years ago
|
try {
|
||
9 years ago
|
return apply(func, undefined, args);
|
||
|
} catch (e) {
|
||
|
return isObject(e) ? e : new Error(e);
|
||
10 years ago
|
}
|
||
|
});
|
||
|
|
||
|
module.exports = attempt;
|