I have 3 questions regarding Svelte Stores:
- How do I make ajax request inside a store? I've tried using the following:
REPL Demo
//store.js
import { writable } from 'svelte/store';
let data = [];
const apiURL = "https://jsonplaceholder.typicode.com/todos";
async function getData(){
const response = await fetch(apiURL);
data = (await response.json()).slice(0,20);
console.log('Response:', data);
}
getData();
export const testsStore = writable(data);
The request goes trough but the data never gets passed to the export. All the examples I've seen use static data without async/await. I've also tried return data;
and writable(getData());
but it return a promise and not the data itself.
Is this even the right way of loading data from API into a store or should I make the call somewhere else.
How and when do I use
export default testsStore;
I tried using it from another example and it throws saying thatstore.js isn't exporting testsStore
{#await}
block in the .svelte file. See this example – Welter