Skip to content

Instantly share code, notes, and snippets.

@max-mapper
Created April 28, 2012 06:04
Show Gist options
  • Save max-mapper/2516455 to your computer and use it in GitHub Desktop.
Save max-mapper/2516455 to your computer and use it in GitHub Desktop.
fs stream backpressure example
var fs = require('fs'),
stream = fs.createWriteStream('blah.txt', { flags: 'w', encoding: 'utf-8' }),
i = 0,
status;
stream.on('open', write)
function write() {
if (i > 100) return stream.end()
console.log(i)
status = stream.write('writing' + i)
i++
while (status) write()
}
stream.on('drain', write)
stream.on('error', function(err) {
console.log("stream error", err)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment