@zlattice/lattice-js
Version:
Lattice blockchain TypeScript SDK with dual module support (CJS + ESM)
22 lines • 832 B
JavaScript
/**
* Retries async operation returned from "func" callback, according to "options".
*/
export 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