2026-05-03 18:23:29 +00:00
|
|
|
export function withTimeout<T>(promise: Promise<T>, ms: number, label?: string): Promise<T> {
|
2025-07-01 02:46:42 +00:00
|
|
|
let timeout: NodeJS.Timeout
|
|
|
|
|
return Promise.race([
|
2026-04-28 23:37:12 +00:00
|
|
|
promise.finally(() => {
|
2025-07-01 02:46:42 +00:00
|
|
|
clearTimeout(timeout)
|
|
|
|
|
}),
|
|
|
|
|
new Promise<never>((_, reject) => {
|
|
|
|
|
timeout = setTimeout(() => {
|
2026-05-03 18:23:29 +00:00
|
|
|
reject(new Error(label ?? `Operation timed out after ${ms}ms`))
|
2025-07-01 02:46:42 +00:00
|
|
|
}, ms)
|
|
|
|
|
}),
|
|
|
|
|
])
|
|
|
|
|
}
|