ai-tube / src /app /api /utils /timeout.ts
jbilcke-hf's picture
jbilcke-hf HF staff
rename from model to entity
1cea837
raw
history blame
No virus
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]);
}