0
0
Svelteframework~5 mins

Await blocks ({#await}) in Svelte - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
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:<br>{#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:<br>{#await promise}<br>Loading...<br>:then data<br>Show data<br>:catch error<br>Show error message<br>{/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>
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
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
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
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
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.