Node.js | Readable Stream pause Event

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

Syntax:

Event: 'pause'

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

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

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the     
// readable pause event
  
// Including fs module
const fs = require('fs');
  
// Constructing readable stream
const readable = fs.createReadStream("input.txt");
readable.on('data', (chunk) => {
  console.log(`${chunk}`);
});
   
// Handling pause event
readable.on("pause", () => {
    console.log("pause emitted!");
});
  
// Calling pause method
readable.pause();
console.log("Program ends....");

chevron_right


Output:

pause emitted!
Program ends....

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the     
// readable pause event
  
// Including fs module
const fs = require('fs');
  
// Constructing readable stream
const readable = fs.createReadStream("input.txt");
readable.on('data', (chunk) => {
  console.log(`${chunk}`);
});
   
// Handling pause event
readable.on("pause", () => {
    console.log("pause emitted!");
});
  
console.log("Program ends....");

chevron_right


Output:

Program ends....
GeeksforGeeks

Here, pause() method is not called so pause event is not emitted.

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




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.