-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathretryBackoff.ts
More file actions
33 lines (32 loc) · 1018 Bytes
/
retryBackoff.ts
File metadata and controls
33 lines (32 loc) · 1018 Bytes
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
27
28
29
30
31
32
33
import runLater from "./runLater.js";
export default function retryBackoff<T>(
op: () => Promise<T>,
delaySeconds: number,
retries: number,
onRetry: (e: any, retries: number) => any,
easing: (delaySeconds: number) => number = (delaySeconds) =>
delaySeconds * 2
): Promise<T> {
return new Promise<T>((resolve, reject) => {
resolve(
op().then(
(result) => result,
(e) => {
if (retries > 0) {
onRetry(e, retries);
return runLater(delaySeconds * 1000, () =>
retryBackoff(
op,
easing(delaySeconds),
retries - 1,
onRetry
)
);
} else {
throw e;
}
}
)
);
});
}