Correct way to handle async data #9677
Replies: 2 comments 2 replies
-
Hi Matthias, the warning is actually a false positive if you are using Firefox or Safari. #7992 (comment) I've just put in a fix for this to try and correct when the warning is emitted #9680 |
Beta Was this translation helpful? Give feedback.
-
This is more of a Svelte thing and not a SvelteKit thing, but what is best practice for rendering mutable async data? <script lang="ts">
let str = "";
let secondStr = "";
$: secondStrPromise = initSecondString(str);
function initSecondString(str: string): Promise<string> {
const promise = testPromise(str);
promise.then((res) => (secondStr = res));
return promise;
}
</script>
<input type="text" bind:value={str} />
{#await secondStrPromise}
<p>Loading...</p>
{:then res}
<p>Immutable promise result: {res}</p>
<input type="text" bind:value={secondStr} />
{:catch e}
<p style="color:crimson;">{e}</p>
{/await} Here I have to split the promise and value into two separate variables, since I can't do One solution is of course to just not use the Anyone has any thoughts on this? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
So in SvelteKit, we are clearly told by a warning that the recommended way to load async data is through "The
fetch
that is passed to yourload
function: https://kit.svelte.dev/docs/load#making-fetch-requests"But there are plenty of situations where you don't want to load everything server-side, such as when loading a lot of data and you want the user to be able to do other stuff in the app while that data is loading.
In the following example, I am using onMount, checking if we are client-side before initiating the fetch request:
But SvelteKit is still warning that I should use the "The
fetch
that is passed to yourload
function: https://kit.svelte.dev/docs/load#making-fetch-requests".So what is it that SvelteKit wants me to do in this situation if loading this server-side is not desirable?
Beta Was this translation helpful? Give feedback.
All reactions