I am confused by some simple behavior I see from readline on()
method.
I have a file called small.csv
which looks like this:
Date,Close
2015-11-12,2045.97
2015-11-11,2075.00
2015-11-10,2081.72
2015-11-09,2078.58
I wrote this script:
my.js
var rl = require('readline').createInterface({
input: require('fs').createReadStream('small.csv')
});
global.myarray = [];
rl.on('line', function (line) {
console.log('Line from file:', line);
global.myarray.push(line);
});
console.log(global.myarray);
// done
Script Output:
dan@nia111:~/d1d2p $
dan@nia111:~/d1d2p $ node -v
v5.0.0
dan@nia111:~/d1d2p $
dan@nia111:~/d1d2p $
dan@nia111:~/d1d2p $ node my.js
[]
Line from file: Date,Close
Line from file: 2015-11-12,2045.97
Line from file: 2015-11-11,2075.00
Line from file: 2015-11-10,2081.72
Line from file: 2015-11-09,2078.58
dan@nia111:~/d1d2p $
dan@nia111:~/d1d2p $
I want to enhance the script so it fills global.myarray
rather than leaving it empty.
When I step through the script with node-debug
,
it appears that global.myarray
is filling but I think that it is an illusion.
Also when I run
node my.js
it appears that
console.log(global.myarray);
runs before small.csv
is read.
So I probably need to understand some asynchronous mechanism at work here.
The following question might be easy for those who understand readline
well.
But, I'd be happy to get an answer to this question:
How to enhance my.js
so it fills global.myarray
rather than leaving it empty?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…