( func: (...args: Args) => Promise<Output>, args: Args, retries: number, )
| 22 | } |
| 23 | |
| 24 | export async function exponentialRetryWrapper<Args extends Array<any>, Output>( |
| 25 | func: (...args: Args) => Promise<Output>, |
| 26 | args: Args, |
| 27 | retries: number, |
| 28 | ): Promise<Output> { |
| 29 | const t1 = Date.now(); |
| 30 | try { |
| 31 | const res = await func(...args); |
| 32 | console.log( |
| 33 | `Exponential retry wrapper completed in ${ |
| 34 | Date.now() - t1 |
| 35 | } ms. Retries remaining: ${retries - 1}`, |
| 36 | ); |
| 37 | return res; |
| 38 | } catch (error) { |
| 39 | console.log(`Error in exponentialRetryWrapper. The error is: ${error}}`); |
| 40 | if (retries > 0) { |
| 41 | console.log(`Retrying ${func.name} in ${2 ** (10 - retries)}ms`); |
| 42 | await new Promise((r) => setTimeout(r, 2 ** (10 - retries))); |
| 43 | return await exponentialRetryWrapper(func, args, retries - 1); |
| 44 | } else { |
| 45 | throw error; |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | export function unpackAndCall( |
| 51 | func: ((...args: any[]) => any) | undefined, |
no outgoing calls
no test coverage detected