(
action: () => PromiseLike<T> | T,
isValid: (result: T) => boolean,
options: WaitForOptions = {},
)
| 12 | } |
| 13 | |
| 14 | export async function waitFor<T>( |
| 15 | action: () => PromiseLike<T> | T, |
| 16 | isValid: (result: T) => boolean, |
| 17 | options: WaitForOptions = {}, |
| 18 | ): Promise<T> { |
| 19 | const timeout = options.timeout ?? 5000; |
| 20 | const interval = options.interval ?? 100; |
| 21 | const startTime = Date.now(); |
| 22 | while (Date.now() - startTime < timeout) { |
| 23 | const result = await action(); |
| 24 | if (isValid(result)) { |
| 25 | return result; |
| 26 | } |
| 27 | await new Promise((resolve) => setTimeout(resolve, interval)); |
| 28 | } |
| 29 | throw new Error(`Timed out waiting for condition after ${timeout}ms`); |
| 30 | } |
no test coverage detected