* Await `promise`, but stop waiting after `timeoutMs`. * * The timeout only bounds how long we WAIT — it does not change the outcome: * - if `promise` settles first, its result is propagated (a rejection throws), * so a cleanup step that actually fails in time still surfaces; * - if the ti
(promise: Promise<void>, timeoutMs: number)
| 47 | * timer is unref'd so it never keeps the loop alive on its own. |
| 48 | */ |
| 49 | async function raceWithTimeout(promise: Promise<void>, timeoutMs: number): Promise<void> { |
| 50 | let timedOut = false; |
| 51 | let timer: ReturnType<typeof setTimeout> | undefined; |
| 52 | // Attach the catch eagerly (synchronously) so `promise` is always consumed and |
| 53 | // a late rejection can never become an unhandled rejection. Before the timeout |
| 54 | // wins, the handler rethrows so a real cleanup failure still propagates. |
| 55 | const guarded = promise.catch((error: unknown) => { |
| 56 | if (timedOut) return; |
| 57 | throw error; |
| 58 | }); |
| 59 | const timedOutSignal = new Promise<void>((resolve) => { |
| 60 | timer = setTimeout(() => { |
| 61 | timedOut = true; |
| 62 | resolve(); |
| 63 | }, timeoutMs); |
| 64 | timer.unref?.(); |
| 65 | }); |
| 66 | try { |
| 67 | await Promise.race([guarded, timedOutSignal]); |
| 68 | } finally { |
| 69 | if (timer !== undefined) clearTimeout(timer); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | interface PromptOutput { |
| 74 | readonly columns?: number | undefined; |
no test coverage detected