mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-11 18:33:53 +00:00
refactor: improve shutdown cleanup
This commit is contained in:
committed by
Carlos Valente
parent
871a6b46c8
commit
4408bdb0d3
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user