@signumjs/util
Version:
Useful utilities and tools for building Signum Network applications
29 lines • 805 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.asyncRetry = asyncRetry;
/**
* Utility function to retry async functions.
*
* @param args The argument object*
*
*/
async function asyncRetry(args) {
const { asyncFn, onFailureAsync, retryCount = 1, maxRetrials = 20 } = args;
try {
return await asyncFn();
// @ts-ignore
}
catch (e) {
if (retryCount > maxRetrials) {
throw e; // cannot recover
}
const shouldRetry = await onFailureAsync(e, retryCount);
if (shouldRetry) {
await asyncRetry({ asyncFn, onFailureAsync, retryCount: retryCount + 1 });
}
else {
throw e; // rethrow most recent error
}
}
}
//# sourceMappingURL=asyncRetry.js.map