Skip to content

Commit

Permalink
fix(streams): erroneous code in WritableStreamDefaultWriter examples (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
nberlette authored Jan 21, 2024
1 parent d26705e commit b3b1738
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ abort(reason)

### Return value

A {{jsxref("Promise")}}, which fulfills with the value given in the `reason`
parameter.
A {{jsxref("Promise")}}, which fulfills to `undefined` when the stream is aborted, or
rejects with an error if the writer was inactive or the receiver stream is invalid.

### Exceptions

Expand Down Expand Up @@ -65,9 +65,7 @@ const writer = writableStream.getWriter();
// ...

// abort the stream when desired
writer.abort.then((reason) => {
console.log(reason);
});
await writer.abort("WritableStream aborted. Reason: ...");
```

## Specifications
Expand Down
13 changes: 7 additions & 6 deletions files/en-us/web/api/writablestreamdefaultwriter/close/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ The following example shows the creation of a `WritableStream` with a custom
sink and an API-supplied queuing strategy. It then calls a function called
`sendMessage()`, passing the newly created stream and a string. Inside this
function it calls the stream's `getWriter()` method, which returns an
instance of {{domxref("WritableStreamDefaultWriter")}}. A `forEach()` call is
used to write each chunk of the string to the stream. Finally, `write()` and
`close()` return promises that are processed to deal with success or failure
of chunks and streams.
instance of {{domxref("WritableStreamDefaultWriter")}}. Each chunk of the
encoded string is written to the stream using the `write()` method, and the
`forEach()` method of the encoded `Uint8Array` to process it byte-by-byte.
Finally, `close()` is called and the Promise it returns is handled to deal
with success (or any failures) of the chunked write operations.

```js
const list = document.querySelector("ul");
Expand All @@ -55,7 +56,7 @@ function sendMessage(message, writableStream) {
// defaultWriter is of type WritableStreamDefaultWriter
const defaultWriter = writableStream.getWriter();
const encoder = new TextEncoder();
const encoded = encoder.encode(message, { stream: true });
const encoded = encoder.encode(message);
encoded.forEach((chunk) => {
defaultWriter.ready
.then(() => {
Expand Down Expand Up @@ -116,7 +117,7 @@ const writableStream = new WritableStream(
sendMessage("Hello, world.", writableStream);
```

You can find the full code in our [Simple writer example](https://mdn.github.io/dom-examples/streams/simple-writer/).
You can view a live demonstration of this at our [simple writer example](https://mdn.github.io/dom-examples/streams/simple-writer/).

## Specifications

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function sendMessage(message, writableStream) {
// defaultWriter is of type WritableStreamDefaultWriter
const defaultWriter = writableStream.getWriter();
const encoder = new TextEncoder();
const encoded = encoder.encode(message, { stream: true });
const encoded = encoder.encode(message);
encoded.forEach((chunk) => {
// Make sure the stream and its writer are able to
// receive data.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function sendMessage(message, writableStream) {
// defaultWriter is of type WritableStreamDefaultWriter
const defaultWriter = writableStream.getWriter();
const encoder = new TextEncoder();
const encoded = encoder.encode(message, { stream: true });
const encoded = encoder.encode(message);
encoded.forEach((chunk) => {
defaultWriter.ready
.then(() => defaultWriter.write(chunk))
Expand Down

0 comments on commit b3b1738

Please sign in to comment.