WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 4fa729d

Browse files
output accumulated stream result instead of undefined value
Updated the sample code to assign result when done is true, since value is always undefined at that point, this ensures the example correctly displays the full accumulated stream output, matching the accompanying explanation.
1 parent 90a1be9 commit 4fa729d

File tree

1 file changed

+5
-2
lines changed
  • files/en-us/web/api/readablestream/getreader

1 file changed

+5
-2
lines changed

files/en-us/web/api/readablestream/getreader/index.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,13 @@ A {{domxref("ReadableStreamDefaultReader")}} or {{domxref("ReadableStreamBYOBRea
4444

4545
In the following simple example, a previously-created custom `ReadableStream` is read using a {{domxref("ReadableStreamDefaultReader")}} created using `getReader()`.
4646
(see our [Simple random stream example](https://mdn.github.io/dom-examples/streams/simple-random-stream/) for the full code).
47-
Each chunk is read sequentially and output to the UI, until the stream has finished being read, at which point we return out of the recursive function and print the entire stream to another part of the UI.
47+
Each chunk is read sequentially and output to the UI, until the stream has finished being read, at which point we return out of the recursive function and display the accumulated stream output to another part of the UI.
4848

4949
```js
5050
function fetchStream() {
5151
const reader = stream.getReader();
5252
let charsReceived = 0;
53+
let result = "";
5354

5455
// read() returns a promise that resolves
5556
// when a value has been received
@@ -59,7 +60,9 @@ function fetchStream() {
5960
// value - some data. Always undefined when done is true.
6061
if (done) {
6162
console.log("Stream complete");
62-
para.textContent = value;
63+
// Output the accumulated stream content instead of `value`,
64+
// which is undefined when `done` is true.
65+
para.textContent = result;
6366
return;
6467
}
6568

0 commit comments

Comments
 (0)