Skip to content

Instantly share code, notes, and snippets.

@bbachi
Created January 24, 2021 03:37
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 bbachi/1d1f77cc2bae57488ed41db5b2cd2ec5 to your computer and use it in GitHub Desktop.
Save bbachi/1d1f77cc2bae57488ed41db5b2cd2ec5 to your computer and use it in GitHub Desktop.
NodeJS Stream
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