Await blocks let you show different content while waiting for something to finish, like loading data. This makes your app feel smooth and clear.
Await blocks ({#await}) in Svelte
{#await promise}
<!-- content while waiting -->
{:then value}
<!-- content when promise resolves -->
{:catch error}
<!-- content if promise rejects -->
{/await}The promise is a JavaScript Promise you want to wait for.
You can show different content for loading, success, and error states.
fetchData() runs, then shows data or error.{#await fetchData()}
<p>Loading...</p>
{:then data}
<p>Data loaded: {data}</p>
{:catch error}
<p>Error: {error.message}</p>
{/await}{#await promise}
<p>Please wait...</p>
{:then result}
<p>Result: {result}</p>
{/await}{#await slowTask()}
<Spinner />
{:then value}
<p>Done: {value}</p>
{:catch err}
<p>Oops! {err.message}</p>
{/await}This Svelte component waits 2 seconds, then shows a success message. While waiting, it shows a loading message. If an error happened, it would show an error message.
<script> let promise = new Promise((resolve, reject) => { setTimeout(() => resolve('Hello from await block!'), 2000); }); </script> {#await promise} <p>Loading message: Please wait 2 seconds...</p> {:then message} <p>Success message: {message}</p> {:catch error} <p>Error message: {error.message}</p> {/await}
Always include a loading state so users know something is happening.
Use the {:catch} block to handle errors gracefully.
Await blocks keep your UI reactive and clean without extra code.
Await blocks help show loading, success, and error states for promises.
They make apps feel responsive and clear to users.
Use them whenever you wait for something that takes time.
Practice
{#await} block in Svelte?Solution
Step 1: Understand the role of
The{#await}in Svelte{#await}block is designed to handle promises and asynchronous data by showing different UI states while waiting, on success, or on error.Step 2: Compare with other options
Creating loops is done with {#each}, defining reactive variables with let or $:, and styling conditionally with class: or style:, which are not the purpose of{#await}.Final Answer:
To handle asynchronous data by showing loading, success, and error states -> Option AQuick Check:
Await blocks = handle async states [OK]
- Confusing await blocks with loops
- Thinking await blocks define reactive variables
- Assuming await blocks are for styling
dataPromise?Solution
Step 1: Recall the basic syntax of await blocks
The correct syntax is{#await promise}followed by the block content and closed with{/await}.Step 2: Analyze each option
{#await dataPromise} ... {/await} matches the correct syntax. {#await (dataPromise)} ... {/await} adds unnecessary parentheses. {#await await dataPromise} ... {/await} repeats 'await' incorrectly. {#await: dataPromise} ... {/await} uses a colon which is invalid.Final Answer:
{#await dataPromise} ... {/await} -> Option DQuick Check:
Correct await syntax = {#await dataPromise} ... {/await} [OK]
- Adding parentheses around the promise
- Repeating the 'await' keyword inside the block
- Using colons after #await
{#await fetchData()}
Loading...
:then data
{data.message}
:catch error
Error: {error.message}
{/await}What will be displayed if
fetchData() returns a promise that resolves to { message: 'Hello!' }?Solution
Step 1: Understand the promise resolution
The promise returned byfetchData()resolves successfully with an object containing{ message: 'Hello!' }.Step 2: Determine which await block section runs
Since the promise resolves, the:thenblock runs, displaying{data.message}which is 'Hello!'.Final Answer:
<p>Hello!</p> -> Option CQuick Check:
Resolved promise shows then block [OK]
- Thinking loading shows after resolution
- Confusing :catch with :then
- Expecting no output on success
{#await loadData()}
Loading...
:then result
{result}
:catch
Error occurred
{/await}Solution
Step 1: Review the :catch syntax in await blocks
The:catchblock must include a variable name to capture the error, like:catch error.Step 2: Check the given code
The code uses:catchwithout a variable name, which is a syntax error.Final Answer:
Missing variable name after :catch -> Option BQuick Check:
:catch needs error variable [OK]
- Omitting error variable in :catch
- Forgetting {/await} closing tag
- Misusing :then keyword
userData promise, display the user's name on success, and show a retry button on error. Which Svelte await block correctly implements this behavior?Solution
Step 1: Verify loading, success, and error states
{#await userData}
:then user
{user.name}
:catch error
Retry
{/await} shows a spinner during loading, displays user name on success with:then user, and shows a retry button with error variableerrorin:catch.Step 2: Check other options for missing parts
{#await userData}
Loading...
:then user
{user.name}
{/await} lacks error handling. {#await userData}
:catch error
Retry
{/await} lacks success handling. {#await userData}
:then user
{user.name}
:catch
Retry
{/await} misses error variable after:catch.Final Answer:
{#await userData}
:then user
{user.name}
:catch error
Retry
{/await} -> Option AQuick Check:
All states with variables = {#await userData}
:then user
{user.name}
:catch error
Retry
{/await} [OK]
- Omitting :catch block or error variable
- Missing :then block for success
- Not showing loading state properly
Ready to prove it?
Pass the test and this pattern moves unknown
