Skip to content

Instantly share code, notes, and snippets.

@Kikobeats
Last active January 27, 2016 08:25
Show Gist options
  • Save Kikobeats/fcf63e33092b2fec26ec to your computer and use it in GitHub Desktop.
Save Kikobeats/fcf63e33092b2fec26ec to your computer and use it in GitHub Desktop.
async stream
var count = 0
function asyncFn (cb) {
setTimeout(function () {
console.log('emit new chunk')
return cb(null, {foo: 'bar'})
}, 500)
}
module.exports = function () {
return from(function (size, next) {
if (count < 5) {
++count
asyncFn(function (err, chunk) {
next(null, chunk)
})
} else {
next(null, null)
}
})
}
emit new chunk
{ foo: 'bar' }
✖ undefined: stream.push() after EOF
@Kikobeats
Copy link
Author

now better using if else instead of while and return

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment