UNPKG

@newdash/newdash

Version:

javascript/typescript utility library

45 lines (44 loc) 1.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.toTry = void 0; /** * create try functions wrapper * * the wrapper will try runners one by one, return the successful value, or the last error * * @since 5.18.0 * @category Fallback * @param runners runners functions */ function toTry(...runners) { return function (...args) { try { const rt = runners[0](...args); if (rt instanceof Promise) { return new Promise((resolve, reject) => { rt .then(resolve) .catch((error) => { if (runners.length === 1) { reject(error); } else { resolve(toTry(...runners.splice(1))(...args)); } }); }); } return rt; } catch (error) { if (runners.length === 1) { throw error; } else { return toTry(...runners.splice(1))(...args); } } }; } exports.toTry = toTry; exports.default = toTry;