* Race a promise against a timeout. On timeout the returned promise REJECTS with * QuoteTimeoutError (so the caller's try/catch can report it) and the timer is * always cleared to avoid leaks. The underlying RPC is abandoned, not cancelled — * gramjs has no cancel — but it no longer blocks the co
(promise: Promise<T>, ms: number, label: string)
| 218 | * gramjs has no cancel — but it no longer blocks the command from completing. |
| 219 | */ |
| 220 | function withTimeout<T>(promise: Promise<T>, ms: number, label: string): Promise<T> { |
| 221 | let timer: NodeJS.Timeout; |
| 222 | const timeout = new Promise<never>((_, reject) => { |
| 223 | timer = setTimeout(() => reject(new QuoteTimeoutError(label, ms)), ms); |
| 224 | // Don't keep the event loop alive solely for this timer. |
| 225 | if (typeof timer.unref === "function") timer.unref(); |
| 226 | }); |
| 227 | return Promise.race([promise, timeout]).finally(() => clearTimeout(timer)) as Promise<T>; |
| 228 | } |
| 229 | |
| 230 | type QuoteArgs = { |
| 231 | count: number; |
no outgoing calls
no test coverage detected