Function
withTimeout
(
promise: Promise<T>,
label: string,
timeoutMs = 3000,
)
Source from the content-addressed store, hash-verified
| 22 | }) |
| 23 | |
| 24 | function withTimeout<T>( |
| 25 | promise: Promise<T>, |
| 26 | label: string, |
| 27 | timeoutMs = 3000, |
| 28 | ): Promise<T> { |
| 29 | let timer: NodeJS.Timeout | undefined |
| 30 | const timeout = new Promise<never>((_, reject) => { |
| 31 | timer = setTimeout(() => reject(new Error(`timed out: ${label}`)), timeoutMs) |
| 32 | }) |
| 33 | return Promise.race([promise, timeout]).finally(() => { |
| 34 | if (timer) clearTimeout(timer) |
| 35 | }) |
| 36 | } |
| 37 | |
| 38 | class FakePeer { |
| 39 | private readonly queued: unknown[] = [] |
Tested by
no test coverage detected