📖 Scenario: You are building a simple Svelte app that fetches a list of messages from a server. The server sends the messages one by one as a stream. You want to show each message as soon as it arrives, without waiting for the entire list.
🎯 Goal: Create a Svelte component that uses a promise to stream messages and display them live as they arrive.
📋 What You'll Learn
Create an array to hold messages
Create a promise that simulates streaming messages
Use await block to handle the promise and update the UI as messages arrive
Display each message inside a list in the component
💡 Why This Matters
🌍 Real World
Streaming data is common in chat apps, live notifications, or real-time dashboards where you want to show data as it arrives without waiting for everything.
💼 Career
Understanding how to handle promises and streams in frameworks like Svelte is important for building responsive, user-friendly web apps that handle asynchronous data efficiently.
Progress0 / 4 steps
1
Set up the messages array
Create a variable called messages and set it to an empty array [] inside the <script> tag.
Svelte
Hint
Use let messages = []; to create an empty array for messages.
2
Create a streaming promise
Create a variable called streamPromise and assign it a new Promise that simulates streaming messages. Inside the promise, use setTimeout to add these messages to messages one by one every 500ms: 'Hello', 'from', 'Svelte'. Resolve the promise after all messages are added.
Svelte
Hint
Use a recursive function with setTimeout inside the promise to add messages one by one.
3
Use await block to show messages
Inside the <ul> tag, use a Svelte {#await} block with streamPromise. While waiting, show <li>Loading...</li>. When resolved, display each message in messages as <li> items using {#each} with message as the iterator variable.
Svelte
Hint
Use {#await streamPromise} with {#each messages as message} inside the :then block to show messages.
4
Add accessibility and finalize
Add an aria-live="polite" attribute to the <ul> tag to announce new messages to screen readers. Also, add a role="list" attribute to the <ul> for better accessibility.
Svelte
Hint
Add aria-live="polite" and role="list" attributes to the <ul> tag for accessibility.
Practice
(1/5)
1. What is the main benefit of using streaming with promises in Svelte when fetching data?
easy
A. It allows showing data bit by bit for faster user feedback.
B. It delays showing any data until the entire response is received.
C. It automatically caches all data without extra code.
D. It prevents any UI updates until the promise fully resolves.
Solution
Step 1: Understand streaming with promises
Streaming lets you process data as it arrives, not waiting for all data.
Step 2: Connect streaming to user experience
Showing data bit by bit gives faster feedback, improving user experience.
Final Answer:
It allows showing data bit by bit for faster user feedback. -> Option A
Quick Check:
Streaming = faster feedback [OK]
Hint: Streaming means showing data as it arrives, not waiting [OK]
Common Mistakes:
Thinking streaming waits for full data
Confusing streaming with caching
Believing UI updates only after full promise resolves
2. Which of the following is the correct way to start reading chunks from a fetch response stream in Svelte?
easy
A. const reader = response.getReader();
B. const reader = response.body.getReader();
C. const reader = response.readableStream();
D. const reader = response.streamReader();
Solution
Step 1: Identify how to access the stream reader
The fetch response has a body property which is a ReadableStream.
Step 2: Use the correct method to get the reader
Calling getReader() on response.body returns the reader.
Final Answer:
const reader = response.body.getReader(); -> Option B
Quick Check:
Use response.body.getReader() [OK]
Hint: Remember: reader comes from response.body.getReader() [OK]
Common Mistakes:
Calling getReader() directly on response
Using non-existent methods like readableStream()
Confusing response.body with response itself
3. Given this Svelte code snippet, what will be logged to the console?
C. Calling getReader() directly on response instead of response.body.
D. Not decoding value before logging.
Solution
Step 1: Check how getReader() is called
getReader() must be called on response.body, not on response itself.
Step 2: Identify the error cause
Calling response.getReader() will cause a runtime error because response has no such method.
Final Answer:
Calling getReader() directly on response instead of response.body. -> Option C
Quick Check:
Use response.body.getReader() [OK]
Hint: getReader() is on response.body, not response [OK]
Common Mistakes:
Calling getReader() on response
Forgetting to decode chunks
Thinking while(true) always causes infinite loop
5. You want to display a loading message that updates with each chunk of streamed data in Svelte. Which approach correctly updates the UI as chunks arrive?
hard
A. Update a local variable inside the async function without reactive binding.
B. Wait for all chunks, then update the UI once with the full data.
C. Use setTimeout to delay UI updates until streaming finishes.
D. Use a writable store to append each decoded chunk and bind it to the UI.
Solution
Step 1: Understand UI reactivity in Svelte
Svelte updates the UI when reactive variables or stores change.
Step 2: Choose a method to update UI per chunk
Using a writable store to append each chunk triggers UI updates as data arrives.
Step 3: Why other options fail
Waiting for all chunks delays UI update; setTimeout delays unnecessarily; local variable changes without binding won't update UI.
Final Answer:
Use a writable store to append each decoded chunk and bind it to the UI. -> Option D
Quick Check:
Writable store + binding = live UI updates [OK]
Hint: Use writable store to update UI reactively per chunk [OK]
Common Mistakes:
Updating local variables without reactive binding
Waiting for full data before UI update
Using timers instead of reactive stores
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.