Interacting with paused streams
A Node.js stream can be in either flowing or paused mode. In flowing mode, data chunks are read automatically, whereas in paused mode, the stream.read() method must be called to read the chunks of data.
In this recipe, we’ll learn how to interact with a readable stream that is in paused mode, which is its default upon creation.
Getting ready
In the learning-streams directory that we created in the previous recipe, create the following file:
$ touch paused-stream.js
We’re now ready to start this recipe.
How to do it…
In this recipe, we’ll learn how to interact with a readable stream that is in paused mode:
- First, import the
fsmodule intopaused-stream.js:const fs = require('node:fs'); - Next, create a readable stream to read the
file.txtfile using thecreateReadStream()method:const rs = fs.createReadStream('./file.txt'); - Next, we need to register a
readableevent handler...