export async function withTimeout < T > ( { promise , timeoutMs , } ) { let timeout ; try { return ( await Promise . race ( [ promise . then ( ( v ) => ( { value : v , timedout : false } ) ) , new Promise ( ( resolve ) => { timeout = setTimeout ( ( ) => resolve ( { timedout : true } ) , timeoutMs ) ; } ) , ] ) ) as Promise < { timedout : true ; } | { timedout : false ; value : T ; } > ; } finally { if ( timeout !== undefined ) { clearTimeout ( timeout ) ; } } } export function isPromise < T > ( maybePromise ) { return maybePromise ? typeof ( maybePromise as Promise < T > ) . then === 'function' : false ; }