Add timeout to fetch calls

This commit is contained in:
Yarmo Mackenbach 2020-08-15 01:03:45 +02:00
parent af87f9aa0b
commit f90059e340

View file

@ -454,7 +454,7 @@ async function verifyProof(url, fingerprint) {
output.qr = url; output.qr = url;
try { try {
response = await fetch(output.proofUrl); response = await fetchWithTimeout(output.proofUrl);
if (!response.ok) { if (!response.ok) {
throw new Error('Response failed: ' + response.status); 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 // General purpose
let elFormVerify = document.body.querySelector("#form-verify"), let elFormVerify = document.body.querySelector("#form-verify"),
elFormEncrypt = document.body.querySelector("#form-encrypt"), elFormEncrypt = document.body.querySelector("#form-encrypt"),