(
condition: () => boolean | Promise<boolean>,
options: {
timeout?: number
interval?: number
message?: string
} = {},
)
| 4 | * Wait for a condition to be true with timeout |
| 5 | */ |
| 6 | export async function waitFor( |
| 7 | condition: () => boolean | Promise<boolean>, |
| 8 | options: { |
| 9 | timeout?: number |
| 10 | interval?: number |
| 11 | message?: string |
| 12 | } = {}, |
| 13 | ): Promise<void> { |
| 14 | const { |
| 15 | timeout = 5000, |
| 16 | interval = 50, |
| 17 | message = `Condition not met`, |
| 18 | } = options |
| 19 | |
| 20 | const start = Date.now() |
| 21 | while (Date.now() - start < timeout) { |
| 22 | if (await condition()) { |
| 23 | return |
| 24 | } |
| 25 | await sleep(interval) |
| 26 | } |
| 27 | |
| 28 | throw new Error(`${message} (timeout after ${timeout}ms)`) |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Sleep for specified milliseconds |
no test coverage detected