export interface RetryConfig { retries : number ; waitInterval : number ; conditions : SkipRetryCondition [ ] ; } export interface SkipRetryCondition { test ( error ) : boolean ; } export class RetryService { constructor ( private config ) { } param func async executeAsyncFunc < T > ( func ) { for ( let i = 0 ; i <= this . config . retries ; i ++ ) { try { const result = await func ( ) ; return result ; } catch ( error ) { const skip = this . config . conditions . map ( condition => condition . test ( error ) ) . find ( skip => skip === true ) ; if ( skip || i === this . config . retries ) { throw error ; } await this . wait ( ) ; } } throw new Error ( 'Wrong retry configuration. Failed to retrieve value.' ) ; } param func async executeSyncFunc < T > ( func ) { for ( let i = 0 ; i <= this . config . retries ; i ++ ) { try { const result = func ( ) ; return result ; } catch ( error ) { const skip = this . config . conditions . map ( condition => condition . test ( error ) ) . find ( skip => skip === true ) ; if ( skip || i === this . config . retries ) { throw error ; } await this . wait ( ) ; } } throw new Error ( 'Wrong retry configuration. Failed to retrieve value.' ) ; } private wait ( ) { return new Promise < void > ( ( res ) : void => { setTimeout ( ( ) => res ( ) , this . config . waitInterval ) ; } ) ; } }