(url, attempts = 60, delayMs = 500)
| 66 | } |
| 67 | |
| 68 | async function waitForJson(url, attempts = 60, delayMs = 500) { |
| 69 | let lastError = null |
| 70 | |
| 71 | for (let index = 0; index < attempts; index += 1) { |
| 72 | try { |
| 73 | const response = await fetch(url) |
| 74 | if (response.ok) { |
| 75 | return await response.json() |
| 76 | } |
| 77 | lastError = new Error(`${url} returned ${response.status}`) |
| 78 | } catch (error) { |
| 79 | lastError = error |
| 80 | } |
| 81 | await sleep(delayMs) |
| 82 | } |
| 83 | |
| 84 | throw lastError || new Error(`等待 ${url} 超时。`) |
| 85 | } |
| 86 | |
| 87 | async function postJson(url, body) { |
| 88 | const response = await fetch(url, { |
no test coverage detected