mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-05 01:17:50 +00:00
Bump to v4.0.0.
This commit is contained in:
33
attempt.js
Normal file
33
attempt.js
Normal file
@@ -0,0 +1,33 @@
|
||||
import apply from './internal/apply';
|
||||
import isError from './isError';
|
||||
import rest from './rest';
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
});
|
||||
|
||||
export default attempt;
|
||||
Reference in New Issue
Block a user