Concept Flow - Await blocks ({#await})
Start: Promise created
Render {#await} block
Check Promise state
Pending
Show pending UI
Update DOM
The await block watches a promise and shows different UI for pending, success, or error states.
<script>
let promise = fetchData();
</script>
{#await promise}
<p>Loading...</p>
{:then data}
<p>Data: {data}</p>
{:catch error}
<p>Error: {error.message}</p>
{/await}| Step | Promise State | Block Rendered | UI Shown | Notes |
|---|---|---|---|---|
| 1 | Pending | Pending block | <p>Loading...</p> | Promise just started, waiting for result |
| 2 | Fulfilled | Then block | <p>Data: {data}</p> | Promise resolved successfully with data |
| 3 | Rejected | Catch block | <p>Error: {error.message}</p> | Promise rejected with error |
| 4 | N/A | End | UI updated accordingly | Rendering finished based on promise state |
| Variable | Start | After Pending | After Fulfilled | After Rejected |
|---|---|---|---|---|
| promise | fetchData() call | pending | resolved with data | rejected with error |
| data | undefined | undefined | actual data value | undefined |
| error | undefined | undefined | undefined | error object |
Await blocks in Svelte watch a promise.
Show pending UI while waiting.
Show then UI on success with data.
Show catch UI on error.
Syntax: {#await promise}...{:then data}...{:catch error}...{/await}