import { IBackoffStrategy } from "./BackoffStrategy" ; import { IPolicy } from "./Policy" ; export class RetryPolicy < T > implements IPolicy < T > { private numFailures : number ; private lastFailure : Error ; constructor ( readonly maxFailures , readonly backoffStrategy ) { this . numFailures = 0 ; } public async execute ( fn ) { while ( this . numFailures < this . maxFailures ) { try { return await fn ( ) ; } catch ( ex ) { this . numFailures += 1 ; this . lastFailure = ex ; await this . backoffStrategy . backoff ( ) ; } } throw this . lastFailure ; } }