Created
December 13, 2014 15:17
-
-
Save nkint/e51b48b31fc1d6f7508e to your computer and use it in GitHub Desktop.
Nodejs slowly output a file line by line
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
// to simulate some recordered arduino output that print a value each 100ms on a serial terminal | |
// thanks to https://github.com/mcollina and https://gist.github.com/walling/ | |
'use strict'; | |
var fs = require('fs'), | |
split2 = require('split2'), | |
through = require('through2') | |
var filename = process.argv[2]; | |
fs.createReadStream(filename) | |
.pipe(split2()) | |
.pipe(slowRelease()) | |
.on('data', function (line) { | |
console.log('.',line); | |
}); | |
function slowRelease() { | |
return through( | |
{ objectMode: true }, | |
function(buffer, encoding, callback) { | |
var self = this; | |
setTimeout(function(buffer) { | |
self.push(buffer); | |
callback(); | |
}, 100, buffer); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment