LoginSignup
0
1

More than 5 years have passed since last update.

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

Posted at

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
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1