Skip to content

Instantly share code, notes, and snippets.

@thospfuller
Created February 26, 2023 15:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thospfuller/65aed7d7cabf0c6a572564d40ddb14ba to your computer and use it in GitHub Desktop.
Save thospfuller/65aed7d7cabf0c6a572564d40ddb14ba to your computer and use it in GitHub Desktop.
This code below was generated by ChatGPT: "Provide a readable streams example using node.js".
/*
* This code below was generated by ChatGPT: "Provide a readable streams example using node.js".
*
* See also:
*
* [1] https://tio.run/##bZLLTsMwEEX3@YrZ2ZFK@gFR2JTuESwRi2kyaQyOXRy7Fary7cEvWqi6sj2Pc2eu/IFHnFojDvZB6Y6WpdVqsnCGF8IOd5JghgYMfTlhiLPJGsKRlXVRrNfwRL1QBHYg6NCiv6CFk5ASdgSplLoiEWNBA29MK2IrYPak4zEYiu9eOxNPcST2nvgbT7AE6PXzMAmakb/R1xj0cEWny9z8XECs4CWEK4AHhuR1XDQGvwFVBwc3DSAsCGV1zGed0BZKq16bLbYD58LSWELzmJngq8VUhf6UqmN4DgZlzefARlDO23JE6bxfGiaxVyijFHl93d@qXrGhMWLnVTFn4zd@fzf@cb43eoyvW6f@e1RpxVlo8E7zdnDq87JLsFRLqqTe50yd5O4Q/MgBcLeXbdM@158SKMvyAw
*/
const { Readable } = require('stream');
// Define the data that will be streamed
const data = ['one', 'two', 'three', 'four', 'five'];
// Create a readable stream
const readableStream = new Readable({
read() {
// Read the data array and push it into the stream
data.forEach((item) => {
this.push(item);
});
// Push a null value to signal the end of the stream
this.push(null);
},
});
// Consume the data from the readable stream
readableStream.on('data', (chunk) => {
console.log(chunk);
});
readableStream.on('end', () => {
console.log('End of stream');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment