Skip to content
Snippets Groups Projects
Commit 0c574d12 authored by julian.baeume's avatar julian.baeume :pick:
Browse files

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.
parent 1987f822
No related branches found
No related tags found
No related merge requests found
......@@ -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
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment