mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-06 17:47:49 +00:00
wip: migrate to bun
This commit is contained in:
30
src/attempt.ts
Normal file
30
src/attempt.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import isError from './isError.js';
|
||||
|
||||
/**
|
||||
* Attempts to invoke `func`, returning either the result or the caught error
|
||||
* object. Any additional arguments are provided to `func` when it's invoked.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @category Util
|
||||
* @param {Function} func The function to attempt.
|
||||
* @param {...*} [args] The arguments to invoke `func` with.
|
||||
* @returns {*} Returns the `func` result or error object.
|
||||
* @example
|
||||
*
|
||||
* // Avoid throwing errors for invalid selectors.
|
||||
* const elements = attempt(selector =>
|
||||
* document.querySelectorAll(selector), '>_>')
|
||||
*
|
||||
* if (isError(elements)) {
|
||||
* elements = []
|
||||
* }
|
||||
*/
|
||||
function attempt(func, ...args) {
|
||||
try {
|
||||
return func(...args);
|
||||
} catch (e) {
|
||||
return isError(e) ? e : new Error(e);
|
||||
}
|
||||
}
|
||||
|
||||
export default attempt;
|
||||
Reference in New Issue
Block a user