Created
October 22, 2015 09:52
-
-
Save yvele/447555b1c5060952a279 to your computer and use it in GitHub Desktop.
Line by line file reading with RxJS on Node.js
This file contains 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
var Rx = require('rx'); | |
var readline = require('readline'); | |
var fs = require('fs'); | |
var rl = readline.createInterface({ | |
input: fs.createReadStream('lines.txt') | |
}); | |
var lines = Rx.Observable.fromEvent(rl, 'line') | |
.takeUntil(Rx.Observable.fromEvent(rl, 'close')) | |
.subscribe( | |
console.log, | |
err => console.log("Error: %s", err), | |
() => console.log("Completed")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
readline
'sInterface
implementsAsyncIterable<string>
to iterate over all lines, and RxJS supportsfrom(AsyncIterable<T>)
since version 7. Therefore, this is now a simple one liner: