( promise: Promise<T>, timeoutMs: number, timeoutValue: T, )
| 26 | * @param timeoutValue - Value to return on timeout |
| 27 | */ |
| 28 | export function withTimeout<T>( |
| 29 | promise: Promise<T>, |
| 30 | timeoutMs: number, |
| 31 | timeoutValue: T, |
| 32 | ): Promise<T> { |
| 33 | let timeoutId: NodeJS.Timeout | null = null |
| 34 | |
| 35 | const timeoutPromise = new Promise<T>((resolve) => { |
| 36 | timeoutId = setTimeout(() => { |
| 37 | resolve(timeoutValue) |
| 38 | }, timeoutMs) |
| 39 | }) |
| 40 | |
| 41 | return Promise.race([promise, timeoutPromise]).finally(() => { |
| 42 | if (timeoutId) { |
| 43 | clearTimeout(timeoutId) |
| 44 | } |
| 45 | }) |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Check if the current terminal supports OSC color queries |
no test coverage detected