Re: [whatwg/streams] Fix readable byte stream example (#1080)

@MattiasBuelens commented on this pull request.

Oh god, that example was very broken indeed. How did we not even check the `done` and `value` properties from the result of `read()`? 😛

The new version already looks much better! 👍 I think I can actually understand what it's doing now. 😁

> -   .catch(e => console.error("Something went wrong!", e));
-
- function readInto(buffer, offset = 0) {
-   if (offset === buffer.byteLength) {
-     return Promise.resolve(buffer);
-   }
+ const buffer = await readInto(startingAB);
+ console.log("The first 1024 bytes: ", buffer);
+
+ async function readInto(buffer) {
+   let offset = 0;
+   let view;
+   let done;
+
+   do {
+     ({value: view, done} =

I think this would read better as:
```javascript
const {value: view, done} = await reader.read(new Uint8Array(buffer, offset, buffer.byteLength - offset));
```
Then we don't need the extra parentheses or the `let view` and `let done` declarations at the top.

> -   .then(buffer => console.log("The first 1024 bytes:", buffer))
-   .catch(e => console.error("Something went wrong!", e));
-
- function readInto(buffer, offset = 0) {
-   if (offset === buffer.byteLength) {
-     return Promise.resolve(buffer);
-   }
+ const buffer = await readInto(startingAB);
+ console.log("The first 1024 bytes: ", buffer);
+
+ async function readInto(buffer) {
+   let offset = 0;
+   let view;
+   let done;
+
+   do {

I *think* this could be a regular `while` loop? If `buffer.byteLength === 0`, then we don't even need to bother calling `read()`.
```suggestion
   while (offset < buffer.byteLength) {
```

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/streams/pull/1080#pullrequestreview-517776597

Received on Tuesday, 27 October 2020 14:46:52 UTC