| 210 | } |
| 211 | |
| 212 | static fetch_(url, action, timeout = 10000) { |
| 213 | const xhr = fetch(url).then((response) => { |
| 214 | const {ok, status, statusText} = response; |
| 215 | if (!ok) { |
| 216 | throw new Error( |
| 217 | `RequestBank.${action}: HTTP ${status} error -- ${statusText}` |
| 218 | ); |
| 219 | } |
| 220 | return response; |
| 221 | }); |
| 222 | if (timeout <= 0) { |
| 223 | return xhr; |
| 224 | } |
| 225 | const timer = new Promise((_, reject) => { |
| 226 | setTimeout(() => { |
| 227 | reject( |
| 228 | new Error(`"RequestBank.${action}" timed out after ${timeout} ms.`) |
| 229 | ); |
| 230 | }, timeout); |
| 231 | }); |
| 232 | return Promise.race([xhr, timer]); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | export class BrowserController { |