export type Deferred < T > = { promise : Promise < T > ; resolve : ( t : T ) => void ; reject : ( e : Error ) => void ; rejectingPromise : Promise < any > ; } ; export function create < T > ( ) { let localResolve = ( ) => { } ; let localReject = ( ) => { } ; const promise = new Promise < T > ( ( resolve , reject ) => { localResolve = resolve ; localReject = reject ; } ) ; const rejectingPromise = promise . then ( ( ) => { throw new Error ( `Promise is always rejecting` ) ; } ) ; rejectingPromise . catch ( ( ) => { } ) ; return { resolve : localResolve , reject : localReject , promise , rejectingPromise , } ; } export function resolveTimeoutPromise < T > ( delay , result ) { return new Promise ( resolve => { setTimeout ( ( ) => { resolve ( result ) ; } , delay ) ; } ) ; } export function rejectTimeoutPromise ( delay , error ) { return new Promise ( ( _resolve , reject ) => { setTimeout ( ( ) => { reject ( error ) ; } , delay ) ; } ) ; }