diff --git a/src/cache.ts b/src/cache.ts index 33cb45e8..fc48fd4d 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -29,7 +29,14 @@ export class CachedFetcher { path += `?${queryParameters}`; } - const call = await fetch(path); + let call: Response; + try { + const timeout = 5000; + call = await this.fetchWithTimeout(path, timeout); + } catch { + console.warn(`Call to cache at ${path} timed out`); + return undefined; + } if (call.status !== 200) { const { url, status, statusText } = call; console.warn(`Call to cache failed at ${url} (status ${status} ${statusText})`); @@ -50,6 +57,13 @@ export class CachedFetcher { return json as T; } + private async fetchWithTimeout(url: string, timeout: number): Promise { + return Promise.race([ + fetch(url), + new Promise((_, reject) => setTimeout(() => reject("timeout"), timeout)) + ]); + } + private get currentValue(): T | undefined { if (!this.expiryDate) { return undefined;