Created
January 24, 2021 03:37
-
-
Save bbachi/1d1f77cc2bae57488ed41db5b2cd2ec5 to your computer and use it in GitHub Desktop.
NodeJS Stream
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
const { Readable } = require('stream'); | |
const myReadbleStream = () => { | |
const data = [ | |
"This is the stream data 1", | |
"This is the stream data 2", | |
"This is the stream data 3", | |
"This is the stream data 4", | |
"This is the stream data 5" | |
]; | |
return new Readable({ | |
read () { | |
if(data.length === 0) { | |
this.push(null); | |
} else { | |
this.push(data.shift()); | |
} | |
} | |
}) | |
} | |
const readableStream = myReadbleStream(); | |
readableStream.on('data', (data) => { | |
console.log('Received Data ', data.toString()); | |
}); | |
readableStream.on('end', () => { | |
console.log('End of data!!!'); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment