UNPKG

1.64 kBTypeScriptView Raw
1export interface AttemptContext {
2 attemptNum: number;
3 attemptsRemaining: number;
4 aborted: boolean;
5 abort: () => void;
6}
7export declare type AttemptFunction<T> = (context: AttemptContext, options: AttemptOptions<T>) => Promise<T>;
8export declare type BeforeAttempt<T> = (context: AttemptContext, options: AttemptOptions<T>) => void;
9export declare type CalculateDelay<T> = (context: AttemptContext, options: AttemptOptions<T>) => number;
10export declare type HandleError<T> = (err: any, context: AttemptContext, options: AttemptOptions<T>) => Promise<void> | void;
11export declare type HandleTimeout<T> = (context: AttemptContext, options: AttemptOptions<T>) => Promise<T>;
12export interface AttemptOptions<T> {
13 readonly delay: number;
14 readonly initialDelay: number;
15 readonly minDelay: number;
16 readonly maxDelay: number;
17 readonly factor: number;
18 readonly maxAttempts: number;
19 readonly timeout: number;
20 readonly jitter: boolean;
21 readonly initialJitter: boolean;
22 readonly handleError: HandleError<T> | null;
23 readonly handleTimeout: HandleTimeout<T> | null;
24 readonly beforeAttempt: BeforeAttempt<T> | null;
25 readonly calculateDelay: CalculateDelay<T> | null;
26}
27export declare type PartialAttemptOptions<T> = {
28 readonly [P in keyof AttemptOptions<T>]?: AttemptOptions<T>[P];
29};
30export declare function sleep(delay: number): Promise<unknown>;
31export declare function defaultCalculateDelay<T>(context: AttemptContext, options: AttemptOptions<T>): number;
32export declare function retry<T>(attemptFunc: AttemptFunction<T>, attemptOptions?: PartialAttemptOptions<T>): Promise<T>;