format import { sleep } from './sleep' ; export function timeout < T > ( ms , promise , timeoutMessage ? ) { const timeout = sleep ( ms ) . then ( ( ) => { throw new Error ( timeoutMessage || `Timed out in ${ ms } ms.` ) ; } ) ; return Promise . race ( [ promise , timeout ] ) ; }