Last active
March 16, 2017 18:39
-
-
Save MarkTiedemann/38e8728ae0ca4c3fa016bb293f356977 to your computer and use it in GitHub Desktop.
Simple Readable Stream Example
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 charArray = 'abcdefghijklmnopqrstuvwxyz'.split('') | |
class AlphabetStream extends Readable { | |
constructor () { | |
super() | |
this.position = 0 | |
} | |
_read () { | |
const index = this.position++ | |
if (index > charArray.length) this.push(null) // end | |
else this.push(charArray[index]) // data | |
} | |
} | |
new AlphabetStream() | |
.on('data', char => console.log(`${char}`)) | |
// a | |
// b | |
// ... | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment