被Node.js readline on()方法混淆 [英] Confused by Node.js readline on() method

查看:65
本文介绍了被Node.js readline on()方法混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对从 readline on()方法中看到的一些简单行为感到困惑.

I am confused by some simple behavior I see from readline on() method.

我有一个名为 small.csv 的文件,如下所示:

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

我写了这个脚本:

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

脚本输出:

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 $ 

我想增强脚本,使其填充 global.myarray 而不是将其保留为空.

I want to enhance the script so it fills global.myarray rather than leaving it empty.

当我使用 node-debug 逐步执行脚本时,似乎 global.myarray 正在填充,但我认为这是一种幻想.

When I step through the script with node-debug, it appears that global.myarray is filling but I think that it is an illusion.

我跑步时 node my.js 看起来 console.log(global.myarray);

在读取 small.csv 之前运行.

所以我可能需要在这里了解一些异步机制.

So I probably need to understand some asynchronous mechanism at work here.

对于那些了解 readline 的人来说,以下问题可能很容易.

The following question might be easy for those who understand readline well.

但是,我很乐意得到这个问题的答案:

But, I'd be happy to get an answer to this question:

如何增强 my.js ,使其填充 global.myarray 而不是将其保留为空?

How to enhance my.js so it fills global.myarray rather than leaving it empty?

推荐答案

由于Node.js具有异步特性,因此在这种情况下可能会有些棘手.这意味着当您的代码正在执行时,它会触发 rl.on('line')处理程序,并传递到下一个调用,在我们的例子中是 console.log .实际代码中的问题不是数组未填满,而是您希望将其填充到早期.这是一个如何解决问题的示例:

Because of it's asynchronous nature Node.js can be a bit tricky with this kind of things. It means that when your code is executing, it is firing the rl.on('line') handler and passing to the next call which in our case is the console.log. The problem in your actual code is not that the array is not filling up, it's that you are expecting it to be populated to early. Here is an example of how you could fix your problem:

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);
});

rl.on('close', function () {
    console.log(global.myarray);
});

在这段代码中,当没有更多行可读取时,将调用 console.log 并显示一些数据.

In this piece of code, when there is no more line to read from, the console.log will be called and there will be some data displayed.

这篇关于被Node.js readline on()方法混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆