import { EventEmitter } from 'events' ; export interface CancelablePromise < T > extends Promise < T > { cancel ( ) : void ; } export const waitEvent = < T = { } > ( emitter , eventName , timeout = 5000 ) : CancelablePromise < T [ ] > => { let timeoutId ; let resolve , reject ; const promise = new Promise < T [ ] > ( ( _resolve , _reject ) => { resolve = _resolve ; reject = _reject ; } ) ; const cleanup = ( ) => { if ( timeoutId ) { clearTimeout ( timeoutId ) ; timeoutId = undefined ; } emitter . removeListener ( eventName , callback ) ; } ; promise . cancel = cleanup ; const callback = ( ... args ) => { cleanup ( ) ; resolve ( args ) ; } ; const timeoutCallback = ( ) => { cleanup ( ) ; reject ( `Timeout waiting for '${ eventName }' response` ) ; } ; emitter . addListener ( eventName , callback ) ; timeoutId = setTimeout ( timeoutCallback , timeout ) ; return promise ; } ;