I would like to receive several data from different APIs and put it together in one big array. This array should be accessable for different components. The components should list the array, if all APIs are called and the data was collected.
I adapted the code from REPL and added a iteration, to call each API. The array will be filled, but the component is not showing the list. First, I thought its related to reactivity / updating arrays, but it also wont work.
import { writable } from 'svelte/store'
export const API_LIST = [
"http://example1.com/api/",
"http://example2.com/api/"
];
export function readAll() {
const { subscribe, update, set } = writable([])
return {
subscribe,
init: async () => {
let alldata = [];
API_LIST.forEach(async function(apiurl) {
const res = await fetch(apiurl)
if (!res.ok) throw new Error('Bad response')
alldata.push( await res.json() );
alldata = alldata;
});
set(alldata);
return alldata;
}
}
}
export const api_store = readAll()
My component:
<script>
import { api_store } from 'reader.js';
</script>
<div>
{#await api_store.init()}
<p>wait for reading...</p>
{:then}
<ul>
{#each $api_store as item}
<li>
{item}
</li>
{/each}
</ul>
{:catch error}
<p>Something went wrong: {error.message}</p>
{/await}
</div>
After all APIs are called and the array is build up, the "wait for reading" disappears, but the list showed is empty. I can see the array content with {console.info($api_store)}, so the data is present. But why is it not showing up in my component list?