What if your app could show data instantly, without waiting for everything to load first?
Why Streaming with promises in Svelte? - Purpose & Use Cases
Imagine you want to show a live feed of data updates on your webpage, like messages or stock prices, but you have to wait for the entire data to load before showing anything.
Waiting for all data before showing anything makes the page feel slow and unresponsive. Manually updating the UI as data arrives is tricky and can cause bugs or flickering.
Streaming with promises lets you show parts of the data as soon as they arrive, making the page feel fast and smooth without complex manual updates.
fetch('/data').then(res => res.json()).then(data => showAll(data))for await (const chunk of streamData()) { showPartial(chunk) }It enables real-time, smooth user experiences by displaying data progressively as it streams in.
Think of a chat app where messages appear instantly as they arrive, instead of waiting for the whole conversation to load.
Manual data loading delays UI updates and feels slow.
Streaming with promises shows data bit by bit as it arrives.
This creates faster, smoother, and more engaging user experiences.
Practice
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 AQuick Check:
Streaming = faster feedback [OK]
- Thinking streaming waits for full data
- Confusing streaming with caching
- Believing UI updates only after full promise resolves
Solution
Step 1: Identify how to access the stream reader
The fetch response has abodyproperty which is a ReadableStream.Step 2: Use the correct method to get the reader
CallinggetReader()onresponse.bodyreturns the reader.Final Answer:
const reader = response.body.getReader(); -> Option BQuick Check:
Use response.body.getReader() [OK]
- Calling getReader() directly on response
- Using non-existent methods like readableStream()
- Confusing response.body with response itself
async function fetchData() {
const response = await fetch('/data');
const reader = response.body.getReader();
const { value, done } = await reader.read();
console.log(new TextDecoder().decode(value));
}
fetchData();Solution
Step 1: Understand reader.read() output
reader.read() returns a promise resolving to an object withvalue(chunk) anddone(boolean).Step 2: Decode the first chunk
The code decodes the first chunk's bytes to text and logs it.Final Answer:
The first chunk of data from the response decoded as text. -> Option AQuick Check:
reader.read() gives first chunk [OK]
- Assuming reader.read() returns full response
- Expecting reader.read() to return undefined value
- Confusing promise with direct value
async function load() {
const response = await fetch('/api');
const reader = response.getReader();
while(true) {
const { value, done } = await reader.read();
if(done) break;
console.log(value);
}
}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 CQuick Check:
Use response.body.getReader() [OK]
- Calling getReader() on response
- Forgetting to decode chunks
- Thinking while(true) always causes infinite loop
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 DQuick Check:
Writable store + binding = live UI updates [OK]
- Updating local variables without reactive binding
- Waiting for full data before UI update
- Using timers instead of reactive stores
Ready to prove it?
Pass the test and this pattern moves unknown
