export function timeout( promise: Promise, ms: number, timeoutError = new Error('Promise timed out') ): Promise { // create a promise that rejects in milliseconds const promiseWithTimeout = new Promise((_, reject) => { setTimeout(() => { reject(timeoutError); }, ms); }); // returns a race between timeout and the passed promise return Promise.race([promise, promiseWithTimeout]); }