From f90059e34015ef4d143e9b7a81d3b5ea5355ea1f Mon Sep 17 00:00:00 2001 From: Yarmo Mackenbach Date: Sat, 15 Aug 2020 01:03:45 +0200 Subject: [PATCH] Add timeout to fetch calls --- static/scripts.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/static/scripts.js b/static/scripts.js index 44c2436..2d78f06 100644 --- a/static/scripts.js +++ b/static/scripts.js @@ -454,7 +454,7 @@ async function verifyProof(url, fingerprint) { output.qr = url; try { - response = await fetch(output.proofUrl); + response = await fetchWithTimeout(output.proofUrl); if (!response.ok) { throw new Error('Response failed: ' + response.status); } @@ -877,6 +877,15 @@ async function generateProfileURL(data) { } } +async function fetchWithTimeout(url, timeout = 3000) { + return Promise.race([ + fetch(url), + new Promise((_, reject) => + setTimeout(() => reject(new Error('timeout')), timeout) + ) + ]); +} + // General purpose let elFormVerify = document.body.querySelector("#form-verify"), elFormEncrypt = document.body.querySelector("#form-encrypt"),