/**
 * Holds the reference and reject callback.
 * On creation these are assigned to a property of an object.
 * If one of 'reject' or 'resolve' is called the property of the object
 * is automatically set to undefined.
 */ export class PromiseCallbacks < T > { public resolve : ( elem : T ) => void ; public reject : ( ( error : Error ) => void ) ; protected obj : Object ; protected property : string ; param obj param property param resolve param reject constructor ( obj , property , resolve , reject ? ) { this . resolve = ( elem ) => { resolve ( elem ) ; this . obj [ this . property ] = undefined ; } ; if ( reject ) { this . reject = ( error ) => { reject ( error ) ; this . obj [ this . property ] = undefined ; } ; } this . obj = obj ; this . property = property ; obj [ property ] = this ; } param timeout public timeoutAfter ( timeout ) { const timeoutHandle = setTimeout ( ( ) => { this . reject ( Error ( "Timeout (" + timeout + "ms)" ) ) ; } , timeout ) ; const prevResolve = this . resolve ; this . resolve = ( elem ) => { clearTimeout ( timeoutHandle ) ; prevResolve ( elem ) ; } ; const prevReject = this . reject ; if ( prevReject ) { this . reject = ( error ) => { clearTimeout ( timeoutHandle ) ; prevReject ( error ) ; } ; } } }