15 lines
433 B
TypeScript
15 lines
433 B
TypeScript
export function withTimeout<T>(promise: Promise<T>, timeoutMs: number): Promise<T> {
|
|
if (!timeoutMs || timeoutMs <= 0) {
|
|
return promise;
|
|
}
|
|
let timer: NodeJS.Timeout | null = null;
|
|
const timeout = new Promise<T>((_, reject) => {
|
|
timer = setTimeout(() => reject(new Error("timeout")), timeoutMs);
|
|
});
|
|
return Promise.race([promise, timeout]).finally(() => {
|
|
if (timer) {
|
|
clearTimeout(timer);
|
|
}
|
|
});
|
|
}
|