0
0
Svelteframework~10 mins

Await blocks ({#await}) in Svelte - Step-by-Step Execution

Choose your learning style9 modes available
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.
Execution Sample
Svelte
  <script>
    let promise = fetchData();
  </script>

  {#await promise}
    <p>Loading...</p>
  {:then data}
    <p>Data: {data}</p>
  {:catch error}
    <p>Error: {error.message}</p>
  {/await}
This code shows loading text while waiting, then data or error after promise settles.
Execution Table
StepPromise StateBlock RenderedUI ShownNotes
1PendingPending block<p>Loading...</p>Promise just started, waiting for result
2FulfilledThen block<p>Data: {data}</p>Promise resolved successfully with data
3RejectedCatch block<p>Error: {error.message}</p>Promise rejected with error
4N/AEndUI updated accordinglyRendering finished based on promise state
💡 Promise settles (fulfilled or rejected), await block updates UI and stops pending state
Variable Tracker
VariableStartAfter PendingAfter FulfilledAfter Rejected
promisefetchData() callpendingresolved with datarejected with error
dataundefinedundefinedactual data valueundefined
errorundefinedundefinedundefinederror object
Key Moments - 3 Insights
Why does the pending block show before the promise resolves?
Because at Step 1 in the execution_table, the promise is still pending, so Svelte renders the pending block UI.
What happens if the promise rejects after showing the pending block?
At Step 3, the promise state changes to rejected, so Svelte switches to render the catch block UI showing the error.
Can the then block run without the promise resolving?
No, as shown in Step 2, the then block only renders after the promise is fulfilled with data.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what UI is shown at Step 1?
A<p>Loading...</p>
B<p>Data: {data}</p>
C<p>Error: {error.message}</p>
DNo UI shown
💡 Hint
Check the 'UI Shown' column for Step 1 in the execution_table
At which step does the promise reject and show an error message?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Promise State' and 'Block Rendered' columns in the execution_table
If the promise never resolves or rejects, which block stays visible?
AThen block
BCatch block
CPending block
DNo block
💡 Hint
Refer to the 'Promise State' and 'Block Rendered' at Step 1 in the execution_table
Concept Snapshot
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}
Full Transcript
In Svelte, the await block lets you handle promises easily in your UI. When you start a promise, the block shows a pending message. Once the promise finishes successfully, it shows the data using the then block. If the promise fails, it shows an error message using the catch block. This flow helps keep your UI responsive and clear about loading states. The execution table shows how the UI changes step by step as the promise state changes from pending to fulfilled or rejected.