From 0c574d1246c09359628bb42fa45149a93424dd3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20B=C3=A4ume?= <julian.baeume@open-xchange.com> Date: Fri, 15 Oct 2021 11:16:42 +0200 Subject: [PATCH] fix fetch and store with multiple locations Promise.race resolve or rejects with the first promise that answers. We now return the first promise that fulfills after all promises have settled. --- src/files.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/files.js b/src/files.js index 6bc4d9c..46ceb80 100644 --- a/src/files.js +++ b/src/files.js @@ -4,7 +4,7 @@ import { config } from './config.js' async function fetchData (path, baseUrl) { const response = await fetch(new URL(path, baseUrl)) - if (!response.ok) return null + if (!response.ok) throw new Error('Error fetching file') const content = await response.buffer() const sha256Sum = crypto.createHash('sha256').update(content).digest('base64') return [path, { @@ -46,7 +46,9 @@ class FileCache { async fetchAndStore (path) { if (config.urls.length === 0) await config.load() - const [key, value] = await Promise.race(config.urls.map(baseUrl => fetchData(path, baseUrl))) + const [[key, value]] = + (await Promise.allSettled(config.urls.map(baseUrl => fetchData(path, baseUrl)))) + .filter(r => r.status === 'fulfilled').map(r => r.value) this._cache[key] = value return value } -- GitLab