UNPKG

@newdash/newdash

Version:

javascript/typescript utility library

39 lines (38 loc) 1.09 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.attempt = void 0; const isError_1 = __importDefault(require("./isError")); /** * 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 5.3.0 * @category Util * @param func The function to attempt. * @param args The arguments to invoke `func` with. * @returns Returns the `func` result or error object. * @example * * ```js * // 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 (0, isError_1.default)(e) ? e : new Error(e); } } exports.attempt = attempt; exports.default = attempt;