a848ef56bf934e6a3174555e174ae22c24e4211a
[yaffs-website] / node_modules / uncss / node_modules / lodash / attempt.js
1 var apply = require('./internal/apply'),
2     isError = require('./isError'),
3     rest = require('./rest');
4
5 /**
6  * Attempts to invoke `func`, returning either the result or the caught error
7  * object. Any additional arguments are provided to `func` when it's invoked.
8  *
9  * @static
10  * @memberOf _
11  * @category Util
12  * @param {Function} func The function to attempt.
13  * @returns {*} Returns the `func` result or error object.
14  * @example
15  *
16  * // avoid throwing errors for invalid selectors
17  * var elements = _.attempt(function(selector) {
18  *   return document.querySelectorAll(selector);
19  * }, '>_>');
20  *
21  * if (_.isError(elements)) {
22  *   elements = [];
23  * }
24  */
25 var attempt = rest(function(func, args) {
26   try {
27     return apply(func, undefined, args);
28   } catch (e) {
29     return isError(e) ? e : new Error(e);
30   }
31 });
32
33 module.exports = attempt;