export interface Deferred < PromiseType > { resolve : ( d : PromiseType ) => void ; reject : ( d : unknown ) => void ; promise : Promise < PromiseType > ; status : 'pending' | 'resolved' | 'rejected' ; } export function defer < PromiseType > ( ) { let resolve ; let reject ; const promise = new Promise < PromiseType > ( ( rs , rj ) => { resolve = ( v ) => { rs ( v ) ; deferred . status = 'resolved' ; } ; reject = ( v ) => { rj ( v ) ; deferred . status = 'rejected' ; } ; } ) ; const deferred = { resolve : resolve ! , reject : reject ! , promise , status : 'pending' , } ; return deferred ; }