cloudaxe-opencode/packages/opencode/src/util/timeout.ts

14 lines
362 B
TypeScript
Raw Normal View History

2025-07-01 02:46:42 +00:00
export function withTimeout<T>(promise: Promise<T>, ms: number): Promise<T> {
let timeout: NodeJS.Timeout
return Promise.race([
promise.finally(() => {
2025-07-01 02:46:42 +00:00
clearTimeout(timeout)
}),
new Promise<never>((_, reject) => {
timeout = setTimeout(() => {
reject(new Error(`Operation timed out after ${ms}ms`))
}, ms)
}),
])
}