Spaces:
Runtime error

llm-router / apps /api /src /services /expense /shared /with-timeout.ts
ClaraSu's picture
Upload folder using huggingface_hub
d958ddc verified
Raw History Blame Contribute Delete
506 Bytes
export async function withTimeout<T>(promise: Promise<T>, ms: number, message: string): Promise<T> {
// The Promise constructor runs its executor synchronously, so `handle` is
// always assigned before the `finally` below ever runs.
let handle: ReturnType<typeof setTimeout>
const timeout = new Promise<never>((_, reject) => {
handle = setTimeout(() => reject(new Error(message)), ms)
})
try {
return await Promise.race([promise, timeout])
} finally {
clearTimeout(handle!)
}
}