Node.js Stream readable.resume() Method
Last Updated :
12 Oct, 2021
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:
const fs = require( 'fs' );
const readable = fs.createReadStream( "input.text" );
readable.on( 'data' , (chunk) => {
console.log(`${chunk}`);
});
readable.pause();
readable.resume();
console.log( "Data starts flowing again!!" );
|
Output:
Data starts flowing again!!
Hello!!!
Example 2:
const fs = require( 'fs' );
const readable = fs.createReadStream( "input.text" );
readable.on( 'data' , (chunk) => {
console.log(`${chunk}`);
readable.pause();
console.log( 'No additional data will be '
+ 'displayed for 3 seconds.' );
setTimeout(() => {
console.log( 'Now data starts flowing again.' );
readable.resume();
}, 3000);
});
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
Share your thoughts in the comments
Please Login to comment...