Last active
August 29, 2015 13:56
-
-
Save supershabam/8800304 to your computer and use it in GitHub Desktop.
promised stream
This file contains hidden or 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
// 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