Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
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]); | |
} |