Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

node.js

What is readable.resume() in the Node.js Stream Module?

Abdul Monum

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

readable.resume()

readable.resume() is used to resume the flow of data elements of an explicitly paused Readable stream. The stream is thus switched to the flowing mode.

When a readable stream is in flowing mode and there is no event handler to consume the data, the data will be lost. For example, when using the readable.resume() method, you should attach a listener to the data event handler to consume the data in order to avoid losing data.

Stream Module in Node.js provides the API for handling the sequence of data flowing over time. The stream types supported in the Node.js Stream Module are:

  • Writable: The stream to which data is written.
  • Readable: The stream from which data is read.
  • Duplex: The stream that is both Readable and Writable.
  • Transform: The stream that is Duplex but can change or transform data as it is read or written.

All of these streams are implemented as classes in the Stream Module. For example, Readable streams follow the interface defined by stream.Readable class. The readable.resume() method is defined in the stream.Readable class.

Readable streams

Readable streams run in two modes: flowing or paused.

  • Flowing mode: Data elements are read from the system asynchronously and provided to the application interface as soon as possible using the Event Emitter interface in Node.js.
  • Paused mode: stream.read() must be called explicitly to read the data elements from the stream.

The following are a few examples of Readable streams:

  • HTTP requests and responses
  • fs read streams
  • TCP sockets
  • zlib streams
  • process.stdin

Example

index.js
example.txt
let fs = require("fs");
const readable = fs.createReadStream('example.txt');
readable.resume().on('end', () => {
console.log('Consumed the data fully, but did not process it.');
});

Explanation

In the above example, we create a readable stream using the fs.createreadStream() method, which uses the data from example.txt. Then, the readable.resume() method is used to fully consume from the readable stream without any processing of the data elements.

RELATED TAGS

node.js

CONTRIBUTOR

Abdul Monum
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring