UNPKG

@geersch/retry

Version:

Backoff strategies to use when retrying a function after a given delay.

27 lines (23 loc) 813 B
import type { BackoffStrategyConfig } from './backoff-strategy-config.js'; import type { BackoffStrategy } from './backoff.strategy.js'; export class FibonacciBackoffStrategy implements BackoffStrategy { private readonly baseDelay: number; private prevDelay: number; private currentDelay: number; constructor({ baseDelay = 100 }: BackoffStrategyConfig = {}) { this.baseDelay = baseDelay; this.prevDelay = 0; this.currentDelay = 1; } *getGenerator(maxRetries: number): Generator<number> { let attempt = 0; while (attempt < maxRetries) { const nextDelay = this.baseDelay * this.currentDelay; yield nextDelay; const sum = this.prevDelay + this.currentDelay; this.prevDelay = this.currentDelay; this.currentDelay = sum; attempt += 1; } } }