( url: string, params: Parameters<typeof fetch>[1], timeoutMs = DEFAULT_FETCH_TIMEOUT_MS, )
| 114 | : 0; |
| 115 | |
| 116 | export const fetchWithTimeout = ( |
| 117 | url: string, |
| 118 | params: Parameters<typeof fetch>[1], |
| 119 | timeoutMs = DEFAULT_FETCH_TIMEOUT_MS, |
| 120 | ): Promise<Response> => { |
| 121 | let timeoutId: ReturnType<typeof setTimeout> | undefined; |
| 122 | |
| 123 | return Promise.race([ |
| 124 | enhancedFetch(url, params), |
| 125 | new Promise<Response>((_, reject) => { |
| 126 | timeoutId = setTimeout(() => { |
| 127 | log('fetch timeout', url); |
| 128 | reject(Error(i18n.t('error_ping_timeout'))); |
| 129 | }, timeoutMs); |
| 130 | }), |
| 131 | ]).finally(() => { |
| 132 | if (timeoutId) { |
| 133 | clearTimeout(timeoutId); |
| 134 | } |
| 135 | }); |
| 136 | }; |
| 137 | |
| 138 | export const enhancedFetch = async ( |
| 139 | url: string, |
no test coverage detected