Posted at

javascript非同期ファイル読み込みサンプル

More than 3 years have passed since last update.

readlineとfsはrequireするだけで使える.

test.js


test.js

var file_path = "test.txt"

var rl = require('readline').createInterface({
input: require('fs').createReadStream( file_path ),
terminal: false
}).on("line", function(line){
console.log( "line: " + line )
}).on("close", function(){
console.log( "Finished file seek" )
})

console.log("Done")


test.txt


test.txt

a

b
c
d
e
f

% node test.js

Done
line: a
line: b
line: c
line: d
line: e
line: f
Finished file seek