jbilcke-hf's picture
jbilcke-hf HF staff
let's try dev mode
3165afb
raw
history blame
432 Bytes
export function timeout<T>(
promise: Promise<T>,
ms: number,
timeoutError = new Error('Promise timed out')
): Promise<T> {
// create a promise that rejects in milliseconds
const promiseWithTimeout = new Promise<never>((_, reject) => {
setTimeout(() => {
reject(timeoutError);
}, ms);
});
// returns a race between timeout and the passed promise
return Promise.race<T>([promise, promiseWithTimeout]);
}