-
Notifications
You must be signed in to change notification settings - Fork 22.5k
/
index.md
93 lines (69 loc) · 2.75 KB
/
index.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
---
title: "ReadableStreamDefaultReader: ReadableStreamDefaultReader() constructor"
short-title: ReadableStreamDefaultReader()
slug: Web/API/ReadableStreamDefaultReader/ReadableStreamDefaultReader
page-type: web-api-constructor
browser-compat: api.ReadableStreamDefaultReader.ReadableStreamDefaultReader
---
{{APIRef("Streams")}}{{AvailableInWorkers}}
The **`ReadableStreamDefaultReader()`**
constructor creates and returns a `ReadableStreamDefaultReader` object
instance.
> [!NOTE]
> You generally wouldn't use this constructor manually; instead,
> you'd use the {{domxref("ReadableStream.getReader()")}} method.
## Syntax
```js-nolint
new ReadableStreamDefaultReader(stream)
```
### Parameters
- `stream`
- : The {{domxref("ReadableStream")}} to be read.
### Return value
An instance of the {{domxref("ReadableStreamDefaultReader")}} object.
### Exceptions
- {{jsxref("TypeError")}}
- : Thrown if the supplied `stream` parameter is not a {{domxref("ReadableStream")}},
or it is already locked for reading by another reader.
## Examples
In the following simple example, a previously-created custom
`ReadableStream` is read using a {{domxref("ReadableStreamDefaultReader")}}
created using `getReader()`. (see our [Simple random stream example](https://mdn.github.io/dom-examples/streams/simple-random-stream/) for the full code). 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.
```js
function fetchStream() {
const reader = stream.getReader();
let charsReceived = 0;
// read() returns a promise that resolves
// when a value has been received
reader.read().then(function processText({ done, value }) {
// Result objects contain two properties:
// done - true if the stream has already given you all its data.
// value - some data. Always undefined when done is true.
if (done) {
console.log("Stream complete");
para.textContent = result;
return;
}
// value for fetch streams is a Uint8Array
charsReceived += value.length;
const chunk = value;
let listItem = document.createElement("li");
listItem.textContent = `Received ${charsReceived} characters so far. Current chunk = ${chunk}`;
list2.appendChild(listItem);
result += chunk;
// Read some more, and call this function again
return reader.read().then(processText);
});
}
```
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}
## See also
- [Streams API concepts](/en-US/docs/Web/API/Streams_API)
- [Using readable streams](/en-US/docs/Web/API/Streams_API/Using_readable_streams)
- {{domxref("ReadableStream")}}
- {{domxref("ReadableStreamDefaultController")}}