Restructure fetchWithTimeout to clear timer on all paths
Replace Promise.race with new Promise wrapper so timeoutId is assigned as const before fetch starts. Timer is now cleared on both fetch success and fetch rejection, not just success.
This commit is contained in:
+10
-10
@@ -17,16 +17,16 @@ const HEALTH_CHECK_TIMEOUT_MS = getTimeout(HOOK_TIMEOUTS.HEALTH_CHECK);
|
|||||||
* The orphaned fetch is harmless since the process exits shortly after.
|
* The orphaned fetch is harmless since the process exits shortly after.
|
||||||
*/
|
*/
|
||||||
export function fetchWithTimeout(url: string, init: RequestInit = {}, timeoutMs: number): Promise<Response> {
|
export function fetchWithTimeout(url: string, init: RequestInit = {}, timeoutMs: number): Promise<Response> {
|
||||||
let timeoutId: ReturnType<typeof setTimeout>;
|
return new Promise((resolve, reject) => {
|
||||||
return Promise.race([
|
const timeoutId = setTimeout(
|
||||||
fetch(url, init).then(response => {
|
() => reject(new Error(`Request timed out after ${timeoutMs}ms`)),
|
||||||
clearTimeout(timeoutId);
|
timeoutMs
|
||||||
return response;
|
);
|
||||||
}),
|
fetch(url, init).then(
|
||||||
new Promise<never>((_, reject) => {
|
response => { clearTimeout(timeoutId); resolve(response); },
|
||||||
timeoutId = setTimeout(() => reject(new Error(`Request timed out after ${timeoutMs}ms`)), timeoutMs);
|
err => { clearTimeout(timeoutId); reject(err); }
|
||||||
}),
|
);
|
||||||
]);
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cache to avoid repeated settings file reads
|
// Cache to avoid repeated settings file reads
|
||||||
|
|||||||
Reference in New Issue
Block a user