tryfunc
Version:
Calling throwing or failing functions
27 lines • 959 B
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const delay_js_1 = __importDefault(require("delay.js"));
exports.default = async (func, validate, { numAttempts, interval, onAttempt, }) => {
if (!Number.isFinite(interval) || interval < 0) {
throw new Error('the interval should be a positive finite integer');
}
for (let i = 0; i < numAttempts; ++i) {
const val = await func(i);
if (validate(val)) {
if (onAttempt) {
onAttempt(val, i, true);
}
return val;
}
const delayPromise = delay_js_1.default(interval);
if (onAttempt) {
onAttempt(val, i, false);
}
await delayPromise;
}
throw new Error('rejected');
};
//# sourceMappingURL=tryUntil.js.map