UNPKG

@andranik-arakelyan/js-utilities

Version:
1 lines 584 B
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.retry=retry;function retry(fn,options={}){const{attempts:attempts=3,delay:delay=1e3,backoffFactor:backoffFactor=2,retryIf:retryIf=()=>true,onRetry:onRetry=()=>{}}=options;let currentAttempt=1;let currentDelay=delay;const execute=async()=>{try{return await fn()}catch(error){if(currentAttempt>=attempts||!retryIf(error)){throw error}onRetry(error,currentAttempt);await new Promise((resolve=>setTimeout(resolve,currentDelay)));currentDelay*=backoffFactor;currentAttempt++;return execute()}};return execute()}