Skip to content

Commit 5b9f6b0

Browse files
committed
minor
1 parent 88cc735 commit 5b9f6b0

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

5-network/03-fetch-progress/article.md

+16-12
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ The `fetch` method allows to track *download* progress.
55

66
Please note: there's currently no way for `fetch` to track *upload* progress. For that purpose, please use [XMLHttpRequest](info:xmlhttprequest), we'll cover it later.
77

8-
To track download progress, we can use `response.body` property. It's a "readable stream" -- a special object that provides body chunk-by-chunk, as it comes.
8+
To track download progress, we can use `response.body` property. It's `ReadableStream` -- a special object that provides body chunk-by-chunk, as it comes. Readable streams are described in the [Streams API](https://streams.spec.whatwg.org/#rs-class) specification.
99

1010
Unlike `response.text()`, `response.json()` and other methods, `response.body` gives full control over the reading process, and we can count how much is consumed at any moment.
1111

@@ -30,14 +30,18 @@ while(true) {
3030
```
3131

3232
The result of `await reader.read()` call is an object with two properties:
33-
- **`done`** -- true when the reading is complete.
33+
- **`done`** -- `true` when the reading is complete, otherwise `false`.
3434
- **`value`** -- a typed array of bytes: `Uint8Array`.
3535

36-
We wait for more chunks in the loop, until `done` is `true`.
36+
```smart
37+
Streams API also describes asynchronous iteration over `ReadableStream` with `for await..of` loop, but it's not yet widely supported (see [browser issues](https://github.com/whatwg/streams/issues/778#issuecomment-461341033)), so we use `while` loop.
38+
```
39+
40+
We receive response chunks in the loop, until the loading finishes, that is: until `done` becomes `true`.
3741

38-
To log the progress, we just need for every `value` add its length to the counter.
42+
To log the progress, we just need for every received fragment `value` to add its length to the counter.
3943

40-
Here's the full code to get response and log the progress, more explanations follow:
44+
Here's the full working example that gets the response and logs the progress in console, more explanations to follow:
4145

4246
```js run async
4347
// Step 1: start the fetch and obtain a reader
@@ -49,7 +53,7 @@ const reader = response.body.getReader();
4953
const contentLength = +response.headers.get('Content-Length');
5054

5155
// Step 3: read the data
52-
let receivedLength = 0; // length at the moment
56+
let receivedLength = 0; // received that many bytes at the moment
5357
let chunks = []; // array of received binary chunks (comprises the body)
5458
while(true) {
5559
const {done, value} = await reader.read();
@@ -84,21 +88,21 @@ Let's explain that step-by-step:
8488

8589
1. We perform `fetch` as usual, but instead of calling `response.json()`, we obtain a stream reader `response.body.getReader()`.
8690

87-
Please note, we can't use both these methods to read the same response. Either use a reader or a response method to get the result.
91+
Please note, we can't use both these methods to read the same response: either use a reader or a response method to get the result.
8892
2. Prior to reading, we can figure out the full response length from the `Content-Length` header.
8993

9094
It may be absent for cross-domain requests (see chapter <info:fetch-crossorigin>) and, well, technically a server doesn't have to set it. But usually it's at place.
9195
3. Call `await reader.read()` until it's done.
9296

93-
We gather response `chunks` in the array. That's important, because after the response is consumed, we won't be able to "re-read" it using `response.json()` or another way (you can try, there'll be an error).
97+
We gather response chunks in the array `chunks`. That's important, because after the response is consumed, we won't be able to "re-read" it using `response.json()` or another way (you can try, there'll be an error).
9498
4. At the end, we have `chunks` -- an array of `Uint8Array` byte chunks. We need to join them into a single result. Unfortunately, there's no single method that concatenates those, so there's some code to do that:
95-
1. We create `new Uint8Array(receivedLength)` -- a same-typed array with the combined length.
96-
2. Then use `.set(chunk, position)` method to copy each `chunk` one after another in the resulting array.
99+
1. We create `chunksAll = new Uint8Array(receivedLength)` -- a same-typed array with the combined length.
100+
2. Then use `.set(chunk, position)` method to copy each `chunk` one after another in it.
97101
5. We have the result in `chunksAll`. It's a byte array though, not a string.
98102

99-
To create a string, we need to interpret these bytes. The built-in [TextDecoder](info:text-decoder) does exactly that. Then we can `JSON.parse` it.
103+
To create a string, we need to interpret these bytes. The built-in [TextDecoder](info:text-decoder) does exactly that. Then we can `JSON.parse` it, if necessary.
100104

101-
What if we need binary content instead of JSON? That's even simpler. Replace steps 4 and 5 with a single call to a blob from all chunks:
105+
What if we need binary content instead of a string? That's even simpler. Replace steps 4 and 5 with a single line that creates a `Blob` from all chunks:
102106
```js
103107
let blob = new Blob(chunks);
104108
```

0 commit comments

Comments
 (0)