mirror of
https://github.com/stablyai/orca.git
synced 2026-11-16 10:58:10 +00:00
* refactor: organize runtime semantic operations by concrete owner * refactor: consolidate terminal tab close type imports * refactor: finish runtime contract owners and mobile catalog snapshot ownership
27 lines
818 B
TypeScript
27 lines
818 B
TypeScript
/** Timeouts and rejections both resolve fallback; callers needing the distinction must handle rejection before passing the promise. */
|
|
export function withTimeout<T>(promise: Promise<T>, timeoutMs: number, fallback: T): Promise<T> {
|
|
let timeout: ReturnType<typeof setTimeout> | null = null
|
|
return new Promise<T>((resolve) => {
|
|
timeout = setTimeout(() => resolve(fallback), timeoutMs)
|
|
promise.then(
|
|
(value) => resolve(value),
|
|
() => resolve(fallback)
|
|
)
|
|
}).finally(() => {
|
|
if (timeout) {
|
|
clearTimeout(timeout)
|
|
}
|
|
})
|
|
}
|
|
|
|
export function withTimeoutResult<T>(
|
|
promise: Promise<T>,
|
|
timeoutMs: number
|
|
): Promise<{ ok: true; value: T } | { ok: false }> {
|
|
return withTimeout(
|
|
promise.then((value) => ({ ok: true, value }) as const),
|
|
timeoutMs,
|
|
{ ok: false }
|
|
)
|
|
}
|