mirror of
https://github.com/cpvalente/ontime.git
synced 2026-09-13 18:19:40 +00:00
16 lines
498 B
TypeScript
16 lines
498 B
TypeScript
/**
|
|
* Resolves or rejects with the provided promise, but fails if it does not settle within `timeoutMs`.
|
|
*/
|
|
export const withTimeout = <T>(promise: Promise<T>, timeoutMs: number): Promise<T> => {
|
|
let timer: NodeJS.Timeout | null = null;
|
|
const timeout = new Promise<T>((_, reject) => {
|
|
timer = setTimeout(() => reject(new Error('Operation timed out')), timeoutMs);
|
|
});
|
|
|
|
return Promise.race([promise, timeout]).finally(() => {
|
|
if (timer) {
|
|
clearTimeout(timer);
|
|
}
|
|
});
|
|
};
|