Open In App

Node.js Stream readable.resume() Method

Last Updated : 12 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

The readable.resume() method in a Readable Stream is used to paused data that can be resumed again and data starts flowing again.

Syntax:

readable.resume()

Parameters: This method doesn’t accept any parameters.

Return Value: If this method is used then the data that was paused, starts flowing again.

Below examples illustrate the use of readable.resume() method in Node.js:

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


Output:

Data starts flowing again!!
Hello!!!

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


Output:

Program ends!!
Hello!!!
No additional data will be displayed for 3 seconds.
Now data starts flowing again.

However, you can see while running, that after the execution of pause() method, no further data will be displayed for 3 seconds.

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads