(fn: () => Promise<T>)
| 59 | * concurrent drainRunLoop() calls share one setInterval. |
| 60 | */ |
| 61 | export async function drainRunLoop<T>(fn: () => Promise<T>): Promise<T> { |
| 62 | retain() |
| 63 | let timer: ReturnType<typeof setTimeout> | undefined |
| 64 | try { |
| 65 | // If the timeout wins the race, fn()'s promise is orphaned — a late |
| 66 | // rejection from the native layer would become an unhandledRejection. |
| 67 | // Attaching a no-op catch swallows it; the timeout error is what surfaces. |
| 68 | // fn() sits inside try so a synchronous throw (e.g. NAPI argument |
| 69 | // validation) still reaches release() — otherwise the pump leaks. |
| 70 | const work = fn() |
| 71 | work.catch(() => {}) |
| 72 | const timeout = withResolvers<never>() |
| 73 | timer = setTimeout(timeoutReject, TIMEOUT_MS, timeout.reject) |
| 74 | return await Promise.race([work, timeout.promise]) |
| 75 | } finally { |
| 76 | clearTimeout(timer) |
| 77 | release() |
| 78 | } |
| 79 | } |
| 80 |
no test coverage detected