Last change
on this file since 76712b2 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
931 bytes
|
Rev | Line | |
---|
[6a3a178] | 1 | var apply = require('./_apply'),
|
---|
| 2 | baseRest = require('./_baseRest'),
|
---|
| 3 | isError = require('./isError');
|
---|
| 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 | * @since 3.0.0
|
---|
| 12 | * @category Util
|
---|
| 13 | * @param {Function} func The function to attempt.
|
---|
| 14 | * @param {...*} [args] The arguments to invoke `func` with.
|
---|
| 15 | * @returns {*} Returns the `func` result or error object.
|
---|
| 16 | * @example
|
---|
| 17 | *
|
---|
| 18 | * // Avoid throwing errors for invalid selectors.
|
---|
| 19 | * var elements = _.attempt(function(selector) {
|
---|
| 20 | * return document.querySelectorAll(selector);
|
---|
| 21 | * }, '>_>');
|
---|
| 22 | *
|
---|
| 23 | * if (_.isError(elements)) {
|
---|
| 24 | * elements = [];
|
---|
| 25 | * }
|
---|
| 26 | */
|
---|
| 27 | var attempt = baseRest(function(func, args) {
|
---|
| 28 | try {
|
---|
| 29 | return apply(func, undefined, args);
|
---|
| 30 | } catch (e) {
|
---|
| 31 | return isError(e) ? e : new Error(e);
|
---|
| 32 | }
|
---|
| 33 | });
|
---|
| 34 |
|
---|
| 35 | module.exports = attempt;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.