Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | 1x 4x 4x 4x 13x 13x 10x 10x 10x 1x | import { IBackoffStrategy } from "./BackoffStrategy";
import { IPolicy } from "./Policy";
export class RetryPolicy<T> implements IPolicy<T> {
private numFailures: number;
private lastFailure: Error;
constructor(readonly maxFailures: number, readonly backoffStrategy: IBackoffStrategy) {
this.numFailures = 0;
}
public async execute(fn: () => Promise<T>): Promise<T> {
while (this.numFailures < this.maxFailures) {
try {
return await fn();
} catch (ex) {
this.numFailures += 1;
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
this.lastFailure = ex;
await this.backoffStrategy.backoff();
}
}
throw this.lastFailure;
}
}
|