* Wait for all promises in the queue to resolve or for timeout to expire, whichever comes first. * * @param timeout The time, in ms, after which to resolve to `false` if the queue is still non-empty. Passing `0` (or * not passing anything) will make the promise wait as long as it takes for
(timeout?: number)
| 67 | * `false` otherwise |
| 68 | */ |
| 69 | function drain(timeout?: number): PromiseLike<boolean> { |
| 70 | if (!buffer.size) { |
| 71 | return resolvedSyncPromise(true); |
| 72 | } |
| 73 | |
| 74 | // We want to resolve even if one of the promises rejects |
| 75 | const drainPromise = Promise.allSettled(Array.from(buffer)).then(() => true); |
| 76 | |
| 77 | if (!timeout) { |
| 78 | return drainPromise; |
| 79 | } |
| 80 | |
| 81 | const promises = [ |
| 82 | drainPromise, |
| 83 | new Promise<boolean>(resolve => safeUnref(setTimeout(() => resolve(false), timeout))), |
| 84 | ]; |
| 85 | |
| 86 | return Promise.race(promises); |
| 87 | } |
| 88 | |
| 89 | return { |
| 90 | get $(): PromiseLike<T>[] { |
nothing calls this directly
no test coverage detected