我正在尝试在 Node.js 下异步和逐行读取文本文件。我有以下 CoffeeScript 代码:
readline = require 'readline'
fs = require 'fs'
#--------------------------------------------------------------------
lines_of = ( route, handler ) ->
#..................................................................
stream = readline.createInterface
input: fs.createReadStream route
output: process.stdout
terminal: false
#..................................................................
stream.on 'close', ->
handler null, null
#..................................................................
stream.on 'error', ( error ) ->
handler error
#..................................................................
stream.on 'line', ( line ) ->
handler null, line
它工作正常,除非最后一行文件不会被报告,除非它被换行符终止。有什么办法可以解决这个问题(当然,除了手动添加换行符)?