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:
16
packages/common/src/fetch-with-timeout.ts
Normal file
16
packages/common/src/fetch-with-timeout.ts
Normal 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);
|
||||
});
|
||||
};
|
||||
@@ -8,3 +8,4 @@ export * from "./url";
|
||||
export * from "./number";
|
||||
export * from "./error";
|
||||
export * from "./encryption";
|
||||
export * from "./fetch-with-timeout";
|
||||
|
||||
Reference in New Issue
Block a user