class ServiceInstance { promise : Promise < void > = null ; } export class ServiceRunner { private services : ServiceInstance [ ] = [ ] ; constructor ( private error = _ => { } ) { } run ( service ) { const instance = new ServiceInstance ( ) ; this . services . push ( instance ) ; try { const promise = service ( ) . then ( ( ) => { this . succeed ( instance ) ; } ) . catch ( x => { this . fail ( instance , x ) ; } ) ; instance . promise = promise ; } catch ( x ) { this . fail ( instance , x ) ; } } all ( ) { return Promise . all ( this . services . filter ( s => s . promise ) . map ( s => s . promise ) ) ; } private succeed ( instance ) { this . remove ( instance ) ; } private fail ( instance , exception ) { this . error ( exception ) ; this . remove ( instance ) ; } private remove ( instance ) { const index = this . services . indexOf ( instance ) ; if ( index >= 0 ) this . services . splice ( index , 1 ) ; } }