0
0
Svelteframework~20 mins

Await blocks ({#await}) in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Await Block Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this Svelte await block render initially?
Consider this Svelte component code:
<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}
AThe text 'Done!' is shown immediately.
BThe text 'Loading...' is shown immediately.
CThe text 'Error: undefined' is shown immediately.
DNothing is shown until the promise resolves.
Attempts:
2 left
💡 Hint
Think about what happens before the promise finishes.
state_output
intermediate
2:00remaining
What is displayed after the promise rejects?
Given this Svelte code:
<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}
AError: Fail
BSuccess: undefined
CWaiting...
DNothing is displayed after rejection.
Attempts:
2 left
💡 Hint
Check the {:catch} block usage.
📝 Syntax
advanced
2: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?
A
{#await promise}
  Loading...
{:then data}
  {data}
{/await}
B
{#await promise}
  Loading...
{:then data}
  {data}
{:else}
  Error
{/await}
C
{#await promise}
  Loading...
{:then data}
  {data}
{:catch error}
  Error
{/await}
D
{#await promise}
  Loading...
{:catch error}
  {error.message}
{:then data}
  {data}
{/await}
Attempts:
2 left
💡 Hint
Remember the order and presence of blocks in await syntax.
🔧 Debug
advanced
2:00remaining
Why does this await block cause a runtime error?
Examine this code:
<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}
AThe loading block must be empty for resolved promises.
BThe promise is not awaited properly in the script.
CThe then block is missing the variable name to receive the resolved value.
DThe catch block is required for all await blocks.
Attempts:
2 left
💡 Hint
Check the syntax of the {:then} block.
🧠 Conceptual
expert
3: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?
ASvelte throws an error because the same promise cannot be awaited twice.
BOnly the first await block updates; the second stays in loading state.
CBoth await blocks cause the promise to run twice, doubling the delay.
DBoth await blocks update independently and show the resolved value without extra waiting.
Attempts:
2 left
💡 Hint
Think about how promises work and how Svelte tracks them.