Skip to content

Instantly share code, notes, and snippets.

@MarkTiedemann
Last active March 16, 2017 18:39
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 MarkTiedemann/38e8728ae0ca4c3fa016bb293f356977 to your computer and use it in GitHub Desktop.
Save MarkTiedemann/38e8728ae0ca4c3fa016bb293f356977 to your computer and use it in GitHub Desktop.
Simple Readable Stream Example
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