mirror of
https://gitea.toothfairyai.com/ToothFairyAI/tf_code.git
synced 2026-03-31 14:22:27 +00:00
15 lines
385 B
TypeScript
15 lines
385 B
TypeScript
export function withTimeout<T>(promise: Promise<T>, ms: number): Promise<T> {
|
|
let timeout: NodeJS.Timeout
|
|
return Promise.race([
|
|
promise.then((result) => {
|
|
clearTimeout(timeout)
|
|
return result
|
|
}),
|
|
new Promise<never>((_, reject) => {
|
|
timeout = setTimeout(() => {
|
|
reject(new Error(`Operation timed out after ${ms}ms`))
|
|
}, ms)
|
|
}),
|
|
])
|
|
}
|