@zlattice/lattice-js
Version:
Lattice blockchain TypeScript SDK with dual module support (CJS + ESM)
25 lines • 935 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.retryAsync = retryAsync;
/**
* Retries async operation returned from "func" callback, according to "options".
*/
function retryAsync(func, options) {
const start = Date.now();
let index = 0;
let e;
let { retry = Number.POSITIVE_INFINITY, delay = -1, error, data } = options ?? {};
const s = () => ({ index, duration: Date.now() - start, error: e, data });
const c = () => func(s()).catch((err) => {
e = err;
typeof error === "function" && error(s());
if ((typeof retry === "function" ? (retry(s()) ? 1 : 0) : retry--) <= 0) {
return Promise.reject(e);
}
const d = typeof delay === "function" ? delay(s()) : delay;
index++;
return d >= 0 ? new Promise((a) => setTimeout(a, d)).then(c) : c();
});
return c();
}
//# sourceMappingURL=retry_async.js.map