WritableStreamDefaultWriter.write()

This is an experimental technology
Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future versions of browsers as the specification changes.

The write() property of the WritableStreamDefaultWriter interface writes a passed chunk of data to a WritableStrame and its underlying sink, then retuurns a Promise that resolves to indicate the success or failure of the write operation.

Syntax

var promise = defatultWriter.write(chunk)

Parameters

chunk
A block of binary data to pass to the WritableStream.

Returns

A Promise that resolves to indicate the success or failure of the write operation.

Example

function sendMessage(message, writableStream) {
  var defaultWriter = writableStream.getWriter();
  var encoder = new TextEncoder();
  var encoded = encoder.encode(message, {stream: true});
  encoded.forEach(function(chunk) {
    // Make sure the stream and its writer are able to
    //   receive data.
    defaultWriter.ready
    .then(function() {
      defaultWriter.write(chunk)
      .then(function() {
        console.log("Chunk written to sink.);
      })
      .catch(function(err) {
        console.log("Chunk error: " + err);
      });
    });
    // Call ready again to ensure that all chunks are written
    //   before closing the writer.
    defaultWriter.ready
    .then(function() {
      defaultWriter.close()
      .then(function() {
        console.log("All chunks written");
      })
      .catch(function(err) {
        console.log("Stream error: " + err);
      });
    });  
  });
}

Specifications

Specification Status Comment
Streams
The definition of 'write()' in that specification.
Living Standard Initial definition.

Browser Compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 58 ? ? 45 ?
Feature Android Webview Chrome for Android Firefox Mobile (Gecko) Firefox OS IE Mobile Opera Mobile Safari Mobile
Basic support 58 58 ? ? ? 45 ?

Document Tags and Contributors

 Contributors to this page: jpmedley
 Last updated by: jpmedley,