( condition: () => boolean | Promise<boolean>, timeoutMs = 5000, pollIntervalMs = 50 )
| 412 | * More robust than fixed sleeps for async operations |
| 413 | */ |
| 414 | export async function waitFor( |
| 415 | condition: () => boolean | Promise<boolean>, |
| 416 | timeoutMs = 5000, |
| 417 | pollIntervalMs = 50 |
| 418 | ): Promise<boolean> { |
| 419 | const startTime = Date.now(); |
| 420 | let currentInterval = pollIntervalMs; |
| 421 | |
| 422 | while (Date.now() - startTime < timeoutMs) { |
| 423 | if (await condition()) { |
| 424 | return true; |
| 425 | } |
| 426 | await new Promise((resolve) => setTimeout(resolve, currentInterval)); |
| 427 | // Exponential backoff with max 500ms |
| 428 | currentInterval = Math.min(currentInterval * 1.5, 500); |
| 429 | } |
| 430 | |
| 431 | return false; |
| 432 | } |
| 433 | |
| 434 | /** |
| 435 | * Wait for a file to exist with retry logic |
no outgoing calls