What is the purpose of the {#await} block in Svelte?
The {#await} block lets you handle promises directly in your Svelte templates. It shows different content while waiting, when resolved, or if an error occurs.
Click to reveal answer
beginner
How do you show a loading message while waiting for a promise in a Svelte {#await} block?
Use the pending part of the block. The content before :then runs while waiting (pending). For example: {#await promise} Loading... {/await}
Click to reveal answer
intermediate
How can you handle errors in a Svelte {#await} block?
Use the :catch block inside {#await}. It shows content if the promise rejects. Example: {#await promise} Loading... :then data Show data :catch error Show error message {/await}
Click to reveal answer
intermediate
What happens if you omit the :then block in a Svelte {#await} block?
If you omit :then, the block only shows the pending or catch content. You won't see the resolved data because there's no place to display it.
Click to reveal answer
beginner
Can you use multiple {#await} blocks in one Svelte component?
Yes, you can use as many {#await} blocks as you want. Each handles its own promise independently, letting you show loading, success, or error states for each async task.
Click to reveal answer
What part of the {#await} block runs while the promise is still loading?
AThe content before <code>:then</code>
BThe content inside <code>:then</code>
CThe content inside <code>:catch</code>
DThe content after <code>{/await}</code>
✗ Incorrect
The content before :then runs while the promise is pending (loading).
How do you access the resolved value of a promise inside a {#await} block?
AInside the <code>:then</code> block with a variable
BInside the <code>:catch</code> block
CBefore the <code>{#await}</code> block
DYou cannot access it
✗ Incorrect
The resolved value is passed as a variable to the :then block.
What does the :catch block do in a {#await} block?
AShows content after the promise resolves
BShows content while the promise is loading
CCancels the promise
DShows content if the promise rejects with an error
✗ Incorrect
The :catch block handles errors if the promise fails.
Is it possible to omit the :catch block in a {#await} block?
ANo, it is required
BYes, but errors won’t be handled in the UI
CYes, and errors will be automatically handled
DNo, it causes a syntax error
✗ Incorrect
You can omit :catch, but then errors won’t show any UI feedback.
Can you nest {#await} blocks inside each other in Svelte?
ANo, nesting is not allowed
BOnly if the promises are related
CYes, to handle multiple async steps
DOnly inside <code>:catch</code> blocks
✗ Incorrect
You can nest {#await} blocks to handle multiple async operations step-by-step.
Explain how to use the {#await} block in Svelte to show loading, success, and error states for a promise.
Think about the three parts of the block: waiting, success, and failure.
You got /3 concepts.
Describe what happens if you omit the :then block in a {#await} block and why you might want to include it.
Consider how the UI changes when the promise finishes.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of the {#await} block in Svelte?
easy
A. To handle asynchronous data by showing loading, success, and error states
B. To create loops for rendering lists
C. To define reactive variables
D. To style components conditionally
Solution
Step 1: Understand the role of {#await} in Svelte
The {#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 A
Quick Check:
Await blocks = handle async states [OK]
Hint: Await blocks manage async loading and errors [OK]
Common Mistakes:
Confusing await blocks with loops
Thinking await blocks define reactive variables
Assuming await blocks are for styling
2. Which of the following is the correct syntax to start an await block in Svelte for a promise named dataPromise?
easy
A. {#await: dataPromise} ... {/await}
B. {#await (dataPromise)} ... {/await}
C. {#await await dataPromise} ... {/await}
D. {#await dataPromise} ... {/await}
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.
Hint: Use {#await promise} without extra symbols [OK]
Common Mistakes:
Adding parentheses around the promise
Repeating the 'await' keyword inside the block
Using colons after #await
3. Given the following Svelte code snippet:
{#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!' }?
medium
A.
Loading...
B.
Error: ...
C.
Hello!
D. Nothing will be displayed
Solution
Step 1: Understand the promise resolution
The promise returned by fetchData() resolves successfully with an object containing { message: 'Hello!' }.
Step 2: Determine which await block section runs
Since the promise resolves, the :then block runs, displaying {data.message} which is 'Hello!'.
Final Answer:
<p>Hello!</p> -> Option C
Quick Check:
Resolved promise shows then block [OK]
Hint: Resolved promise shows :then content [OK]
Common Mistakes:
Thinking loading shows after resolution
Confusing :catch with :then
Expecting no output on success
4. Identify the error in this Svelte await block:
{#await loadData()}
Loading...
:then result
{result}
:catch
Error occurred
{/await}
medium
A. Incorrect use of :then keyword
B. Missing variable name after :catch
C. No closing {/await} tag
D. Promises cannot be used in await blocks
Solution
Step 1: Review the :catch syntax in await blocks
The :catch block must include a variable name to capture the error, like :catch error.
Step 2: Check the given code
The code uses :catch without a variable name, which is a syntax error.
Final Answer:
Missing variable name after :catch -> Option B
Quick Check:
:catch needs error variable [OK]
Hint: Always name error after :catch [OK]
Common Mistakes:
Omitting error variable in :catch
Forgetting {/await} closing tag
Misusing :then keyword
5. You want to show a loading spinner while waiting for userData promise, display the user's name on success, and show a retry button on error. Which Svelte await block correctly implements this behavior?
hard
A. {#await userData}
:then user
{user.name}
:catch error Retry {/await}
B. {#await userData}
Loading...
:then user
{user.name}
{/await}
C. {#await userData}
:catch error Retry {/await}
D. {#await userData}
:then user
{user.name}
:catch Retry {/await}
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 variable error in :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 A
Quick Check:
All states with variables = {#await userData}
:then user
{user.name}
:catch error Retry {/await} [OK]
Hint: Include loading, then, catch with variables [OK]
Common Mistakes:
Omitting :catch block or error variable
Missing :then block for success
Not showing loading state properly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.