當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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'事件。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。