当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Node.js stream.Writable 'drain'事件用法及代码示例


事件:'drain'

添加于:v0.9.4

如果对 stream.write(chunk) 的调用返回 false ,则在适合继续将数据写入流时将发出 'drain' 事件。

// Write the data to the supplied writable stream one million times.
// Be attentive to back-pressure.
function writeOneMillionTimes(writer, data, encoding, callback) {
  let i = 1000000;
  write();
  function write() {
    let ok = true;
    do {
      i--;
      if (i === 0) {
        // Last time!
        writer.write(data, encoding, callback);
      } else {
        // See if we should continue, or wait.
        // Don't pass the callback, because we're not done yet.
        ok = writer.write(data, encoding);
      }
    } while (i > 0 && ok);
    if (i > 0) {
      // Had to stop early!
      // Write some more once it drains.
      writer.once('drain', write);
    }
  }
}

相关用法


注:本文由纯净天空筛选整理自nodejs.org大神的英文原创作品  'drain'事件。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。