export async function retryAction()

in fuse-ui-shared/decorators/retry.ts [103:125]


export async function retryAction<T>(action: () => Promise<T>, canRetry: (e: any) => Promise<boolean>, delay: Delayable) {
  const start = new Date().getTime();
  let { type, beat, maxDuration, maxInterval, maxIteration } = _.extend({}, _defaultRetryOption, delay);
  let sequence = createSequence(type);
  let invokeCount = 0;

  return repeat(action, canRetry, async () => {
    const now = new Date().getTime();
    invokeCount++;
    const duration = now - start;
    if (maxIteration > 0 && invokeCount >= maxIteration) {
      throw new Error(`exceeded maxIteration ${maxIteration}`);
    }
    let waitDuration = sequence.next() * beat;
    if (maxInterval > 0) {
      waitDuration = Math.min(maxInterval, waitDuration);
    }
    if (maxDuration > 0 && duration + waitDuration > maxDuration) {
      throw new Error(`exceeded maxDuration ${maxDuration}`);
    }
    await sleep(waitDuration);
  });
}