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