| 23 | * Executes the given function and retries on failure. |
| 24 | */ |
| 25 | export function execWithRetry<T>(func: () => Promise<T>, retryOptions: RetryOptions = { retryCount: 3, retryWaitTimeInMs: 3000 }): Promise<T> { |
| 26 | return func().catch((error1) => { |
| 27 | if (retryOptions.retryCount > 0) { |
| 28 | return new Promise<T>((resolve, reject) => { |
| 29 | setTimeout(() => { |
| 30 | retryOptions.retryCount--; |
| 31 | execWithRetry(func, retryOptions).then((response) => { |
| 32 | resolve(response); |
| 33 | }).catch((error2) => { |
| 34 | reject(error2); |
| 35 | }); |
| 36 | }, retryOptions.retryWaitTimeInMs); |
| 37 | }); |
| 38 | } else { |
| 39 | return Promise.reject(error1); |
| 40 | } |
| 41 | }); |
| 42 | } |
| 43 | } |