Skip to content

Instantly share code, notes, and snippets.

Created March 29, 2013 20:57
Show Gist options
  • Save anonymous/5273601 to your computer and use it in GitHub Desktop.
Save anonymous/5273601 to your computer and use it in GitHub Desktop.
Node.js Readable example
// rs is a stream.Readable
rs.on('readable', function () {
while (true) {
var chunk = rs.read();
if (chunk === null) break;
// do something with chunk
}
});
@isaacs
Copy link

isaacs commented Mar 29, 2013

I'd consider while (true) to be at least a red flag, if not an outright bug most of the time.

This is somewhat clearer, imo:

var chunk;
while (null !== (chunk = rs.read())) {
  doSomething(chunk);
}

Of course if your goal is just to pull data out asap, you may as well use rs.on('data', function(chunk) { ... }) which basically does exactly that.

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