0
0
Svelteframework~20 mins

Streaming with promises in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Streaming with Promises Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What will this Svelte component render initially?
Consider this Svelte component that uses a promise to fetch data and streams the result. What will the user see on the page before the promise resolves?
Svelte
<script>
  let dataPromise = new Promise((resolve) => {
    setTimeout(() => resolve('Hello from promise!'), 2000);
  });
</script>

<p>{#await dataPromise}
  Loading...
{:then data}
  {data}
{/await}</p>
ALoading...
BHello from promise!
CAn error message
DNothing is rendered
Attempts:
2 left
💡 Hint
Think about what the #await block shows before the promise finishes.
state_output
intermediate
2:00remaining
What is the final displayed text after promise resolves?
This Svelte component streams a promise that resolves after 1 second. What text will be shown after the promise resolves?
Svelte
<script>
  let promise = new Promise((res) => setTimeout(() => res('Data loaded'), 1000));
</script>

{#await promise}
  <p>Waiting...</p>
{:then result}
  <p>{result}</p>
{/await}
A<p>undefined</p>
B<p>Data loaded</p>
C<p>Waiting...</p>
DNo content rendered
Attempts:
2 left
💡 Hint
What does the {:then} block do?
📝 Syntax
advanced
2:00remaining
Which option correctly streams a promise in Svelte?
You want to show a loading message while waiting for a promise and then show the data once it resolves. Which code snippet correctly uses Svelte's #await block?
A
{#await fetchData}
  &lt;p&gt;Loading...&lt;/p&gt;
{:then data}
  &lt;p&gt;{data}&lt;/p&gt;
{/await}
B
{#await fetchData()}
  &lt;p&gt;Loading...&lt;/p&gt;
{:then}
  &lt;p&gt;{data}&lt;/p&gt;
{/await}
C
{#await fetchData()}
  &lt;p&gt;Loading...&lt;/p&gt;
{:catch error}
  &lt;p&gt;{error}&lt;/p&gt;
{/await}
D
{#await fetchData()}
  &lt;p&gt;Loading...&lt;/p&gt;
{:then data}
  &lt;p&gt;{data}&lt;/p&gt;
{/await}
Attempts:
2 left
💡 Hint
Remember to call the function returning the promise and use the correct syntax for then block.
🔧 Debug
advanced
2:00remaining
Why does this Svelte component throw an error?
This component tries to stream a promise but throws an error. What is the cause?
Svelte
<script>
  let promise = fetch('/api/data').then(res => res.json());
</script>

{#await promise}
  <p>Loading...</p>
{:then data}
  <p>{data.message}</p>
{/await}
AThe API response does not have a 'message' property, causing undefined error
BThe promise is not awaited properly inside the script tag
CThe #await block syntax is incorrect and missing curly braces
DThe fetch call must be inside onMount lifecycle hook
Attempts:
2 left
💡 Hint
Check what the API returns and if the property accessed exists.
🧠 Conceptual
expert
2:00remaining
What happens if a promise inside a Svelte #await block rejects without a catch?
Consider a Svelte component using a promise inside an #await block. The promise rejects but there is no {:catch} block. What will the user see?
Svelte
<script>
  let promise = new Promise((_, reject) => setTimeout(() => reject('Failed'), 1000));
</script>

{#await promise}
  <p>Loading...</p>
{:then data}
  <p>{data}</p>
{/await}
AThe component will display nothing after rejection
BThe component will automatically show the rejection message
CThe component will crash and show an error in the console
DThe component will display 'Loading...' forever
Attempts:
2 left
💡 Hint
Think about how Svelte handles promise rejections without a catch block.