* Wait for the next occurrence of an event.
(event: string, timeout?: number)
| 172 | * Wait for the next occurrence of an event. |
| 173 | */ |
| 174 | async waitFor<T = any>(event: string, timeout?: number): Promise<T> { |
| 175 | return new Promise((resolve, reject) => { |
| 176 | const timeoutMs = timeout ?? this.options.timeout; |
| 177 | let timeoutId: ReturnType<typeof setTimeout> | null = null; |
| 178 | |
| 179 | const cleanup = this.once(event, (data: T) => { |
| 180 | if (timeoutId) { |
| 181 | clearTimeout(timeoutId); |
| 182 | } |
| 183 | resolve(data); |
| 184 | }); |
| 185 | |
| 186 | timeoutId = setTimeout(() => { |
| 187 | cleanup(); |
| 188 | reject(new Error(`Timeout waiting for event '${event}' after ${timeoutMs}ms`)); |
| 189 | }, timeoutMs); |
| 190 | }); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Remove listeners. |
no test coverage detected