@contrail/util 1.1.1-0 → 1.1.1-1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export interface RetryConfig {
|
|
2
2
|
maxRetries?: number;
|
|
3
3
|
initialDelay?: number;
|
|
4
|
-
|
|
4
|
+
onRetryCallback?: (error: Error, attempt: number) => Promise<void> | void;
|
|
5
5
|
}
|
|
6
6
|
export declare function retry<T>(fn: () => Promise<T>, config?: RetryConfig): Promise<T>;
|
|
@@ -16,7 +16,7 @@ const DEFAULT_CONFIG = {
|
|
|
16
16
|
};
|
|
17
17
|
function retry(fn, config = DEFAULT_CONFIG) {
|
|
18
18
|
return __awaiter(this, void 0, void 0, function* () {
|
|
19
|
-
const { maxRetries
|
|
19
|
+
const { maxRetries, initialDelay, onRetryCallback } = Object.assign(Object.assign({}, DEFAULT_CONFIG), config);
|
|
20
20
|
if (maxRetries < 1) {
|
|
21
21
|
throw new Error('maxRetries must be at least 1');
|
|
22
22
|
}
|
|
@@ -27,15 +27,13 @@ function retry(fn, config = DEFAULT_CONFIG) {
|
|
|
27
27
|
}
|
|
28
28
|
catch (error) {
|
|
29
29
|
attempt++;
|
|
30
|
-
if (onRetry)
|
|
31
|
-
onRetry(error, attempt);
|
|
32
30
|
if (attempt === maxRetries) {
|
|
33
|
-
|
|
34
|
-
throw new Error(`Max retries reached: ${error.message}`);
|
|
31
|
+
throw error;
|
|
35
32
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
33
|
+
if (onRetryCallback) {
|
|
34
|
+
yield onRetryCallback(error, attempt);
|
|
35
|
+
}
|
|
36
|
+
yield new Promise((resolve) => setTimeout(resolve, initialDelay * attempt));
|
|
39
37
|
}
|
|
40
38
|
}
|
|
41
39
|
throw new Error('Max retries reached');
|