Challenge - 5 Problems
Await Block Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What does this Svelte await block render initially?
Consider this Svelte component code:
What will the user see immediately after the component loads?
<script>
let promise = new Promise(resolve => setTimeout(() => resolve('Done!'), 2000));
</script>
{#await promise}
Loading...
{:then result}
{result}
{:catch error}
Error: {error.message}
{/await}What will the user see immediately after the component loads?
Svelte
let promise = new Promise(resolve => setTimeout(() => resolve('Done!'), 2000)); {#await promise} Loading... {:then result} {result} {:catch error} Error: {error.message} {/await}
Attempts:
2 left
💡 Hint
Think about what happens before the promise finishes.
✗ Incorrect
The await block shows the first block (the loading state) while the promise is pending. So 'Loading...' appears immediately.
❓ state_output
intermediate2:00remaining
What is displayed after the promise rejects?
Given this Svelte code:
What will the component display after 1 second?
<script>
let promise = new Promise((_, reject) => setTimeout(() => reject(new Error('Fail')), 1000));
</script>
{#await promise}
Waiting...
{:then data}
Success: {data}
{:catch err}
Error: {err.message}
{/await}What will the component display after 1 second?
Svelte
let promise = new Promise((_, reject) => setTimeout(() => reject(new Error('Fail')), 1000)); {#await promise} Waiting... {:then data} Success: {data} {:catch err} Error: {err.message} {/await}
Attempts:
2 left
💡 Hint
Check the {:catch} block usage.
✗ Incorrect
When the promise rejects, the {:catch} block runs and shows the error message 'Fail'.
📝 Syntax
advanced2:00remaining
Which option correctly uses an await block with only a then block?
You want to show a loading message while waiting and then show data when ready, but no catch block. Which code is correct?
Attempts:
2 left
💡 Hint
Remember the order and presence of blocks in await syntax.
✗ Incorrect
The await block can have a loading block and a then block. The catch block is optional but must come after then if present. Option A correctly omits catch.
🔧 Debug
advanced2:00remaining
Why does this await block cause a runtime error?
Examine this code:
What causes the error when rendering?
<script>
let promise = Promise.resolve('Hello');
</script>
{#await promise}
Loading...
{:then}
{data}
{/await}What causes the error when rendering?
Svelte
let promise = Promise.resolve('Hello'); {#await promise} Loading... {:then} {data} {/await}
Attempts:
2 left
💡 Hint
Check the syntax of the {:then} block.
✗ Incorrect
The {:then} block must specify a variable name to hold the resolved value. Missing it causes a runtime error because {data} is undefined.
🧠 Conceptual
expert3:00remaining
How does Svelte handle multiple await blocks with the same promise?
If you use the same promise in two separate {#await} blocks in a component, what happens when the promise resolves?
Attempts:
2 left
💡 Hint
Think about how promises work and how Svelte tracks them.
✗ Incorrect
A promise is a single object representing one future value. Multiple await blocks using the same promise share its state and update when it resolves.