Skip to content

Instantly share code, notes, and snippets.

@nkint
Created December 13, 2014 15:17
Show Gist options
  • Save nkint/e51b48b31fc1d6f7508e to your computer and use it in GitHub Desktop.
Save nkint/e51b48b31fc1d6f7508e to your computer and use it in GitHub Desktop.
Nodejs slowly output a file line by line
// 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