export function delay ( durationMs ) { return new Promise ( ( resolve ) => setTimeout ( resolve , durationMs ) ) ; } export async function waitUntil < T extends unknown > ( delayMs , tries , test ) { let result = tries > 0 && await test ( ) ; while ( tries > 0 && ! result ) { tries = tries - 1 ; await delay ( delayMs ) ; result = await test ( ) ; } if ( ! result ) throw new Error ( `Wait loop failed` ) ; else return result as Exclude < T , false > ; } export interface Deferred < T > { resolve : ( arg : T ) => void ; reject : ( e ? : Error ) => void ; promise : Promise < T > ; } export function getDeferred < T = void > ( ) { let resolve = undefined ; let reject = undefined ; let promise = new Promise < T > ( ( resolveCb , rejectCb ) => { resolve = resolveCb ; reject = rejectCb ; } ) ; return { resolve , reject , promise } as any ; }