可讀流中的可讀.resume()方法用於暫停可再次恢複的數據,然後數據再次開始流動。
用法:
readable.resume()
參數:此方法不接受任何參數。
返回值:如果使用此方法,那麽暫停的數據將再次開始流動。
下麵的示例說明了Node.js中read.resume()方法的使用:
範例1:
// Node.js program to demonstrate the
// readable.resume() method
// Including fs module
const fs = require('fs');
// Constructing readable stream
const readable = fs.createReadStream("input.text");
readable.on('data', (chunk) => {
console.log(`${chunk}`);
});
// Calling pause method
readable.pause();
// Calling resume method
readable.resume();
console.log("Data starts flowing again!!");
輸出:
Data starts flowing again!! Hello!!!
範例2:
// Node.js program to demonstrate the
// readable.resume() method
// Include fs module
const fs = require('fs');
// Create readable stream
const readable = fs.createReadStream("input.text");
// 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();
}, 3000);
});
// Displays that program
// is ended
console.log("Program ends!!");
輸出:
Program ends!! Hello!!! No additional data will be displayed for 3 seconds. Now data starts flowing again.
但是,您可以看到在運行時,執行pause()方法後,在3秒鍾內將不會顯示其他數據。
參考: https://nodejs.org/api/stream.html#stream_readable_resume
相關用法
- node.js Stream writable.end()用法及代碼示例
- node.js Stream readable.read()用法及代碼示例
- node.js Stream writable.setDefaultEncoding()用法及代碼示例
- node.js Stream writable.cork()用法及代碼示例
- node.js Stream readable.isPaused()用法及代碼示例
- node.js Stream readable.pause()用法及代碼示例
- node.js Stream readable.setEncoding()用法及代碼示例
- node.js Stream readable.pipe()用法及代碼示例
- node.js Stream writable.write()用法及代碼示例
- node.js Stream transform.destroy()用法及代碼示例
- node.js Stream readable.unpipe()用法及代碼示例
- node.js Stream writable.destroy()用法及代碼示例
- node.js Stream writable.uncork()用法及代碼示例
- node.js Stream readable.destroy()用法及代碼示例
注:本文由純淨天空篩選整理自nidhi1352singh大神的英文原創作品 Node.js | Stream readable.resume() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。