( promise: Promise<T>, ms: number, message: string, )
| 68 | * returns control to the caller. |
| 69 | */ |
| 70 | export function withTimeout<T>( |
| 71 | promise: Promise<T>, |
| 72 | ms: number, |
| 73 | message: string, |
| 74 | ): Promise<T> { |
| 75 | let timer: ReturnType<typeof setTimeout> | undefined |
| 76 | const timeoutPromise = new Promise<never>((_, reject) => { |
| 77 | // eslint-disable-next-line no-restricted-syntax -- not a sleep: REJECTS after ms (timeout guard) |
| 78 | timer = setTimeout(rejectWithTimeout, ms, reject, message) |
| 79 | if (typeof timer === 'object') timer.unref?.() |
| 80 | }) |
| 81 | return Promise.race([promise, timeoutPromise]).finally(() => { |
| 82 | if (timer !== undefined) clearTimeout(timer) |
| 83 | }) |
| 84 | } |
| 85 |
nothing calls this directly
no outgoing calls
no test coverage detected