WritableStreamDefaultWriter.ready - Web APIs 编辑

Experimental

This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

The ready read-only property of the WritableStreamDefaultWriter interface returns a Promise that resolves when the desired size of the stream's internal queue transitions from non-positive to positive, signaling that it is no longer applying backpressure.

Syntax

var promise = writableStreamDefaultWriter.ready;

Value

A Promise.

Example

The following example shows two uses of the ready property. The first uses ready to ensure that the WritableStream is done writing and thus able to receive data before sending a binary chunk. The second also checks whether the the WritableStream is done writing, but this time because the writing must be finished before the writer can be closed.

function sendMessage(message, writableStream) {
  // defaultWriter is of type WritableStreamDefaultWriter
  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

SpecificationStatusComment
Streams
The definition of 'ready' in that specification.
Living StandardInitial definition.

Browser Compatibility

BCD tables only load in the browser

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:70 次

字数:3417

最后编辑:7年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文