From 43fa551a64b125a87eec5bb50bf935dd8b22ece9 Mon Sep 17 00:00:00 2001 From: Danny Coates Date: Sun, 13 Aug 2017 18:44:59 -0700 Subject: [PATCH] improved exist check --- frontend/src/fileList.js | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/frontend/src/fileList.js b/frontend/src/fileList.js index d03d4ddf..ce5f1fff 100644 --- a/frontend/src/fileList.js +++ b/frontend/src/fileList.js @@ -13,16 +13,20 @@ document.addEventListener('DOMContentLoaded', function() { fileList = document.getElementById('file-list'); toggleHeader(); // eslint-disable-next-line prefer-const - for (let file of storage.files) { - const id = file.fileId; - checkExistence(id).then(exists => { - if (exists) { - addFile(storage.getFileById(id)); - } else { - storage.remove(id); - } - }); - } + Promise.all( + storage.files.map(file => { + const id = file.fileId; + return checkExistence(id).then(exists => { + if (exists) { + addFile(storage.getFileById(id)); + } else { + storage.remove(id); + } + }); + }) + ) + .catch(err => console.error(err)) + .then(toggleHeader); }); function toggleHeader() { @@ -209,11 +213,14 @@ async function checkExistence(id) { return new Promise((resolve, reject) => { const xhr = new XMLHttpRequest(); xhr.onreadystatechange = () => { - if (xhr.readyState === XMLHttpRequest.DONE) { + if (xhr.readyState === XMLHttpRequest.HEADERS_RECEIVED) { resolve(xhr.status === 200); } }; + xhr.onerror = reject; + xhr.ontimeout = reject; xhr.open('get', '/exists/' + id); + xhr.timeout = 2000; xhr.send(); }); }