Cannot push to an array with Node EventEmitter .on method

Sunday, July 6, 2014

Using readLine to read from a file I have:



fs = require 'fs'
readline = require 'readline'
stream = require 'stream'

instream = fs.createReadStream './test.txt'
outstream = new stream
outstream.readable = true
outstream.writable = true

rl = readline.createInterface({input: instream, output: outstream, terminal: false})

arr = []

rl.on 'line', (line) ->
arr.push line

console.log arr


I'm trying to push each line to an array, but the callback in rl.on doesn't seem to complete the push() method, but if I replace the push with console.log it will print every line of the file. I realise there are probably far better ways of achieving the same outcome, but this is an experiment that failed and I don't understand why.


Why is the .on callback not able to push the line to an array?







http://ift.tt/1mlnbAy