The
readable.unshift() method in a Readable Stream is utilized to push a chunk of data back into the internal buffer. However, when a stream is being fully consumed and it needs to be "un-consumed" then this method is helpful.
Syntax:
readable.unshift( chunk, encoding )
Parameters: This method accept two parameters as mentioned above and described below.
- chunk: It contains the chunk of data to unshift onto the read queue.
- encoding: It holds the encoding type.
Return Value: This method shifts the chunk of data to the internal buffer which can be read-only after calling read method again.
Below examples illustrate the use of
readable.unshift() method in Node.js:
Example 1:
Output:
Program ends!!!
true
read: GfG
Example 2:
Output:
Unshifted!!
Reference: https://nodejs.org/api/stream.html#stream_readable_unshift_chunk_encoding.