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


Node.js Readable Stream resume事件用法及代碼示例


可讀流中的 ‘resume’ 事件在調用 stream.resume() 並且 readableFlowing 屬性不為真時發出。

用法:

Event:'resume'

返回值:如果 readable.resume() 正在被調用,則它會被發出,否則它不會被發出。

下麵的例子說明了在 Node.js 中 resume 事件的使用:

範例1:




// Node.js program to demonstrate the     
// readable resume event
  
// Including fs module
const fs = require('fs');
  
// Constructing readable stream
const readable = fs.createReadStream("input.txt");
  
// Handling data event
readable.on('data', (chunk) => {
  console.log(`${chunk}`);
});
  
// Calling pause method
 readable.pause() 
  
 // Calling resume method
 readable.resume();
  
 // Handling resume event
 readable.on('resume', () => {
    console.log("resume emitted!");
});
  
console.log("Program ends....");

輸出:

Program ends....
resume emitted!
GeeksforGeeks

範例2:


// Node.js program to demonstrate the     
// readable resume event
  
// Including fs module
const fs = require('fs');
  
// Constructing readable stream
const readable = fs.createReadStream("input.txt");
  
// Handling data event
readable.on('data', (chunk) => {
console.log(`${chunk}`);
  
  // Calling pause method
  readable.pause();
  
  // After this any data will be displayed 
  // after 3 sec.
  console.log('No additional data will "
        + "be displayed for 3 seconds.');
  
  // Using setTimeout function
  setTimeout(() => {
    console.log('Now data starts flowing again.');
  
    // Calling resume method
    readable.resume();
  
    // Emitting resume event
    readable.on("resume", () => {
    console.log("resume emitted!");
});
  }, 3000);
});
  
console.log("Done....");

輸出:

Done....
GeeksforGeeks
No additional data will be displayed for 3 seconds.
Now data starts flowing again.
resume emitted!

參考: https://nodejs.org/api/stream.html#stream_event_resume




相關用法


注:本文由純淨天空篩選整理自nidhi1352singh大神的英文原創作品 Node.js | Readable Stream resume Event。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。