main
Last change
on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago |
Update repo after prototype presentation
|
-
Property mode
set to
100644
|
File size:
929 bytes
|
Rev | Line | |
---|
[d565449] | 1 | import apply from './_apply.js';
|
---|
| 2 | import baseRest from './_baseRest.js';
|
---|
| 3 | import isError from './isError.js';
|
---|
| 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 | export default attempt;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.