Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

WritableStream

BaselineWidely available *

Note: This feature is available inWeb Workers.

TheWritableStream interface of theStreams API provides a standard abstraction for writing streaming data to a destination, known as a sink.This object comes with built-in backpressure and queuing.

WritableStream is atransferable object.

Constructor

WritableStream()

Creates a newWritableStream object.

Instance properties

WritableStream.lockedRead only

A boolean indicating whether theWritableStream is locked to a writer.

Instance methods

WritableStream.abort()

Aborts the stream, signaling that the producer can no longer successfully write to the stream and it is to be immediately moved to an error state, with any queued writes discarded.

WritableStream.close()

Closes the stream.

WritableStream.getWriter()

Returns a new instance ofWritableStreamDefaultWriter and locks the stream to that instance. While the stream is locked, no other writer can be acquired until this one is released.

Examples

The following example illustrates several features of this interface. It creates theWritableStream with a custom sink. It then calls the stream'sgetWriter() method, which returns an instance ofWritableStreamDefaultWriter. Next, several strings are written to the stream. Finally,close() returns a promise that resolves when all the writes have successfully completed.

js
const writableStream = new WritableStream(  // Implement the sink  {    write(chunk) {      const textElement = document.getElementById("text-output");      textElement.textContent += chunk;    },  },);const writer = writableStream.getWriter();try {  writer.write("Hello, ");  writer.write("world!\n");  writer.write("This has been a demo!\n");  await writer.close(); // wait for all chunks to be written  console.log("All chunks written");} catch (error) {  console.error("Stream error: ", error);}

This example does not support thebackpressure feature of Streams.

Specifications

Specification
Streams
# ws-class

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp