UNPKG

@shanyue/promise-utils

Version:

Userful promise utils, include map, filter, retry and sleep

39 lines 1.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.retry = exports.AbortError = void 0; class AbortError extends Error { constructor(message) { super(); if (message instanceof Error) { this.originalError = message; ({ message } = message); } else { this.originalError = new Error(message); this.originalError.stack = this.stack; } this.name = 'AbortError'; this.message = message; } } exports.AbortError = AbortError; async function retry(run, { times = 10, onFailedAttempt = () => { }, } = {}) { let count = 1; async function exec() { try { const result = await run(count); return result; } catch (e) { if (count > times || e instanceof AbortError) { throw e; } count++; await onFailedAttempt(e); return exec(); } } return exec(); } exports.retry = retry; //# sourceMappingURL=retry.js.map