feat: add 5s timeout for network requests

This commit is contained in:
rugk 2022-06-12 21:09:55 +02:00
parent 62905a913c
commit 809773f0ca
No known key found for this signature in database
GPG key ID: 05D40A636AFAB34D

View file

@ -28,6 +28,7 @@ function convertUrlToString(url) {
* A small fetch wrapper.
*
* Just adjusts fetch request, so they are common enough.
* Also adds a timeout of five seconds to avoid stuck fetch requests in case of a server not responding.
*
* @public
* @param {USVString|Request} input
@ -57,8 +58,24 @@ export function fetch(input, init = {}, ...args) {
}
}
// add timeout value if needed
// from https://stackoverflow.com/a/50101022/5008962
const abortController = new AbortController();
let abortTimeoutId;
if (!init.signal) {
abortTimeoutId = setTimeout(() => abortController.abort(), 5000);
init.signal = abortController.signal;
}
// continue with global fetch
return window.fetch(input, init, ...args);
return window.fetch(input, init, ...args).then((x) => {
if (abortTimeoutId) {
// do not timeout the response, only the request
clearTimeout(abortTimeoutId);
}
return x;
});
}
/**