Node.js | Readable Stream resume Event

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:



filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Program ends....
resume emitted!
GeeksforGeeks

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.