import { injectable } from 'inversify' ; @ injectable ( ) export class PromiseUtils { public async waitFor < T , Y > ( promise , timeoutMsec , onTimeoutCallback ) { let timeoutHandle ; let hasTimedOut = false ; const timeoutPromise = new Promise < void > ( ( resolve ) => { timeoutHandle = setTimeout ( ( ) => { hasTimedOut = true ; resolve ( ) ; } , timeoutMsec ) ; } ) ; const racePromise = Promise . race ( [ promise , timeoutPromise ] ) ; try { await racePromise ; } finally { clearTimeout ( timeoutHandle ) ; } if ( hasTimedOut ) { return onTimeoutCallback ( ) ; } return promise ; } }