(predicate: () => T, opts?: { timeout_seconds?: number; check_period_ms?: number })
| 54 | } |
| 55 | |
| 56 | export function waitFor<T>(predicate: () => T, opts?: { timeout_seconds?: number; check_period_ms?: number }) { |
| 57 | return new Promise<T>((resolve, reject) => { |
| 58 | let timeout: any; |
| 59 | const interval = setInterval(() => { |
| 60 | const result = predicate(); |
| 61 | if (result) { |
| 62 | clearInterval(interval); |
| 63 | timeout && clearTimeout(timeout); |
| 64 | resolve(result); |
| 65 | } |
| 66 | }, opts?.check_period_ms || 1000); |
| 67 | |
| 68 | // 超时跳过 |
| 69 | if (opts?.timeout_seconds) { |
| 70 | timeout = setTimeout(() => { |
| 71 | clearInterval(interval); |
| 72 | resolve(undefined as any); |
| 73 | }, (opts?.timeout_seconds || 10) * 1000); |
| 74 | } |
| 75 | }); |
| 76 | } |
no outgoing calls
no test coverage detected