Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Node.js Readable Stream resume Event

  • Last Updated : 12 Oct, 2021

The ‘resume’ Event in a Readable Stream is emitted when stream.resume() is being called and readableFlowing property is not true.

Syntax:

Event: 'resume'

Return Value: It is emitted if readable.resume() is being called else it is not emitted.

Below examples illustrate the use of resume event in Node.js:

Example 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....");

Output:

Program ends....
resume emitted!
GeeksforGeeks

Example 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....");

Output:

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

Reference: https://nodejs.org/api/stream.html#stream_event_resume




My Personal Notes arrow_drop_up
Recommended Articles
Page :