Skip to content

Instantly share code, notes, and snippets.

@supershabam
Last active August 29, 2015 13:56
Show Gist options
  • Save supershabam/8800304 to your computer and use it in GitHub Desktop.
Save supershabam/8800304 to your computer and use it in GitHub Desktop.
promised stream
// backpressure is better modeled with a promised event
var stream
// our data listeners are functions that *may* return a promise
// when the promise resolves, the data has been consumed and the stream may continue
stream.on('data', function(data) {
// simulate being busy by returning a promise that resolves in 150ms
return new Promise(function(resolve, reject) {
setTimeout(function() {
console.log('laggy-stream: ', data)
resolve()
}, 150)
})
})
// a separate stream consumer with no lag
stream.on('data', function(data) {
console.log('no-delay-stream: ', data)
// returning null (or a non-promise value signifies we're done with data)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment