diff options
Diffstat (limited to 'packages/taler-wallet-webextension/src/utils')
-rw-r--r-- | packages/taler-wallet-webextension/src/utils/index.ts | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/packages/taler-wallet-webextension/src/utils/index.ts b/packages/taler-wallet-webextension/src/utils/index.ts index 8eb89d58f..88f9bc4b3 100644 --- a/packages/taler-wallet-webextension/src/utils/index.ts +++ b/packages/taler-wallet-webextension/src/utils/index.ts @@ -43,14 +43,37 @@ export async function queryToSlashConfig<T>( .then(getJsonIfOk); } +function timeout<T>(ms: number, promise: Promise<T>): Promise<T> { + return new Promise((resolve, reject) => { + const timer = setTimeout(() => { + reject(new Error(`Timeout: the query took longer than ${Math.floor(ms / 1000)} secs`)) + }, ms) + + promise + .then(value => { + clearTimeout(timer) + resolve(value) + }) + .catch(reason => { + clearTimeout(timer) + reject(reason) + }) + }) +} + export async function queryToSlashKeys<T>( url: string, ): Promise<T> { - return fetch(new URL("keys", url).href) + const endpoint = new URL("keys", url) + endpoint.searchParams.set("cacheBreaker", new Date().getTime() + ""); + + const query = fetch(endpoint.href) .catch(() => { throw new Error(`Network error`); }) .then(getJsonIfOk); + + return timeout(3000, query) } export function buildTermsOfServiceState(tos: GetExchangeTosResult): TermsState { |