1
- export interface IRetryOperation {
1
+ interface RetryOperation {
2
2
retryCount : number | "infinite" ;
3
3
retryDelay : number ;
4
4
retryCallback : ( payload ?: any ) => any ;
@@ -8,6 +8,12 @@ export interface IRetryOperation {
8
8
incrementalDelayFactor ?: number ; // Optional factor to increase the delay
9
9
}
10
10
11
+ interface RetryAsyncOperationExtended extends RetryOperation {
12
+ retryAsyncCallback : ( ) => Promise < void > ;
13
+ }
14
+
15
+ type RetryAsyncOperation = Omit < RetryAsyncOperationExtended , "retryCallback" > ;
16
+
11
17
const sleep = ( delay : number ) => {
12
18
return new Promise ( ( resolve ) => setTimeout ( resolve , delay ) ) ;
13
19
} ;
@@ -20,7 +26,7 @@ async function retryOperation({
20
26
onSuccessCallback,
21
27
afterLastAttemptErrorCallback,
22
28
incrementalDelayFactor = 1 , // Default factor is 1 (no increment)
23
- } : IRetryOperation ) {
29
+ } : RetryOperation ) {
24
30
let noOfRetries = 0 ;
25
31
let lastError : any = null ;
26
32
let currentDelay = retryDelay ;
@@ -39,7 +45,7 @@ async function retryOperation({
39
45
return ;
40
46
} catch ( error ) {
41
47
lastError = error ;
42
- onErrorCallback ( error ) ;
48
+ onErrorCallback ( error as Error ) ;
43
49
currentDelay *= incrementalDelayFactor ; // Increase the delay
44
50
}
45
51
}
@@ -48,11 +54,6 @@ async function retryOperation({
48
54
afterLastAttemptErrorCallback ( lastError ) ;
49
55
}
50
56
}
51
- interface TRetryAsyncOperationF extends IRetryOperation {
52
- retryAsyncCallback : ( ) => Promise < void > ;
53
- }
54
-
55
- type TRetryAsyncOperation = Omit < TRetryAsyncOperationF , "retryCallback" > ;
56
57
57
58
async function retryAsyncOperation ( {
58
59
retryCount,
@@ -62,7 +63,7 @@ async function retryAsyncOperation({
62
63
onSuccessCallback,
63
64
afterLastAttemptErrorCallback,
64
65
incrementalDelayFactor = 1 , // Default factor is 1 (no increment)
65
- } : TRetryAsyncOperation ) {
66
+ } : RetryAsyncOperation ) {
66
67
let noOfRetries = 0 ;
67
68
let lastError : any = null ;
68
69
let currentDelay = retryDelay ;
@@ -81,7 +82,7 @@ async function retryAsyncOperation({
81
82
return ;
82
83
} catch ( error ) {
83
84
lastError = error ;
84
- onErrorCallback ( error ) ;
85
+ onErrorCallback ( error as Error ) ;
85
86
currentDelay *= incrementalDelayFactor ; // Increase the delay
86
87
}
87
88
}
@@ -91,4 +92,9 @@ async function retryAsyncOperation({
91
92
}
92
93
}
93
94
94
- export { retryOperation , retryAsyncOperation } ;
95
+ export {
96
+ retryOperation ,
97
+ retryAsyncOperation ,
98
+ RetryOperation ,
99
+ RetryAsyncOperation ,
100
+ } ;
0 commit comments