Created
February 26, 2023 15:04
-
-
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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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