What if you could handle loading and errors in your UI with just a few lines of code?
Why Await blocks ({#await}) in Svelte? - Purpose & Use Cases
Imagine you want to show data from the internet on your webpage. You write code to fetch it, but while waiting, the page stays blank or freezes, confusing your users.
Manually handling loading states means writing extra code to track when data arrives, show loading messages, and handle errors. This is slow, easy to forget, and makes your code messy.
Svelte's {#await} blocks let you declare what to show while waiting, when data arrives, or if an error happens--all in one clean place. It handles the timing for you automatically.
let data; fetch(url).then(r => r.json()).then(d => { data = d; updateUI(); }); showLoading();{#await fetch(url).then(r => r.json())} <p>Loading...</p> {:then data} <p>{data.title}</p> {:catch error} <p>Error!</p> {/await}You can easily create smooth user experiences that show loading and error messages without complicated code.
When you open a news app, it shows a spinner while loading articles, then displays them or an error if something goes wrong--all handled cleanly with {#await}.
Manual loading states are hard and messy.
{#await} blocks simplify asynchronous UI updates.
They improve user experience with minimal code.