import { injectable } from 'inversify' ; import Util from '../util/Util' ; import IPromiseRetry , { RetryOption } from './IPromiseRetry' ; @ injectable ( ) export default class PromiseRetry implements IPromiseRetry { param job param option public async run < T > ( job , option ) { let error = null ; const cnt = typeof option === 'undefined' || typeof option . cnt === 'undefined' ? 5 : option . cnt ; for ( let i = 0 ; i < cnt ; i ++ ) { try { const result = await job ( ) ; return result ; } catch ( err ) { error = err ; await Util . sleep ( typeof option === 'undefined' || typeof option . waitTime === 'undefined' ? 1000 : option . waitTime ) ; continue ; } } if ( error === null ) { throw new Error ( 'ExecutePromiseRetryError' ) ; } throw error ; } }