fix: fetch timeout for external requests to small (#881)

* fix: fetch timeout for external requests to small

* fix: format issue

* fix: move clear timeout for fetch to finally
This commit is contained in:
Meier Lukas
2024-07-29 06:30:56 +02:00
committed by GitHub
parent 406ed16ce4
commit 67dad45214
9 changed files with 34 additions and 7 deletions

View File

@@ -0,0 +1,16 @@
/**
* Same as fetch, but with a timeout of 10 seconds.
* https://stackoverflow.com/questions/46946380/fetch-api-request-timeout
* @param param0 fetch arguments
* @returns fetch response
*/
export const fetchWithTimeout = (...[url, requestInit]: Parameters<typeof fetch>) => {
const controller = new AbortController();
// 10 seconds timeout:
const timeoutId = setTimeout(() => controller.abort(), 10000);
return fetch(url, { signal: controller.signal, ...requestInit }).finally(() => {
clearTimeout(timeoutId);
});
};