import { logger } from "." ; import { RetryState } from "../models" ; param { Function } aFunction param { RetryState } retryState returns { Promise < T > } export const retryFunctionWithState = async < T > ( aFunction , retryState ) : Promise < T > => { retryState . registerRetry ( ) ; try { return await new Promise ( ( resolve , reject ) => { setTimeout ( async ( ) => { logger . warn ( `retrying after ${ retryState . currentInterval }ms...` ) ; try { resolve ( await aFunction ( ) ) ; } catch ( error ) { reject ( error ) ; } } , retryState . currentInterval ) ; } ) ; } catch ( error ) { return retryFunctionWithState < T > ( aFunction , retryState ) ; } } ;