(
cb: () => ReturnValue | Promise<ReturnValue>,
{
errorMessage,
timeout = 5000,
}: { errorMessage?: string; timeout?: number } = {},
)
| 141 | * throw the error message provided as a fallback |
| 142 | */ |
| 143 | export async function waitFor<ReturnValue>( |
| 144 | cb: () => ReturnValue | Promise<ReturnValue>, |
| 145 | { |
| 146 | errorMessage, |
| 147 | timeout = 5000, |
| 148 | }: { errorMessage?: string; timeout?: number } = {}, |
| 149 | ) { |
| 150 | const endTime = Date.now() + timeout |
| 151 | let lastError: unknown = new Error(errorMessage) |
| 152 | while (Date.now() < endTime) { |
| 153 | try { |
| 154 | const response = await cb() |
| 155 | if (response) return response |
| 156 | } catch (e: unknown) { |
| 157 | lastError = e |
| 158 | } |
| 159 | await new Promise((r) => setTimeout(r, 100)) |
| 160 | } |
| 161 | throw lastError |
| 162 | } |
no outgoing calls
no test coverage detected