Files
orca/src/shared/promise-timeout-fallback.ts
T
Neil 439e28b8a2 refactor: organize runtime semantic operations by concrete owner (#38602)
* 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
2026-11-05 10:00:28 -08:00

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 }
)
}