Node and Lazy:我怎么知道它何时完成?

时间:2012-06-20 11:03:42

标签: javascript node.js iasyncresult

我需要逐行读取文件,并相应地更改变量。 我通常会用PHP写这个...但我决定接受挑战。

我写道:

fs = require('fs');
Lazy = require('lazy');
path = require('path');

files = fs.readdirSync('.');
var software = {};


files.forEach( function(fileName){


  var m; 
  if( m = fileName.match(/^(.*)\.txt$/) ){
    name = m[1];

    console.log("Processing file: " + fileName);
    software[name] = {};
    console.log("Software 1: %j",software);

    var section = 'unset';
    new Lazy(fs.createReadStream(fileName)).lines.forEach(
      function(line){
        var m;
        line = line + '';
        if( m = line.match(/^([a-zA-Z_]*):$/)){
          section = m[1];
          software[name][section] = '';
          console.log("Switching to section " + m[1]);
          console.log("Software 2: %j",software);
        } else if (line == '.'){
          section = 'unset'
        } else if (line == ''){
          section = 'unset'
        } else { 
          console.log("LINE: " + line) ;
          software[name][section] = software[name][section] + line + "\n";
          console.log("Software 3: %j",software);
        }
      }

    );   
  }

});

console.log("Software 4: %j",software);

除了代码非常难看且非常不优化之外,我遇到了麻烦,因为当最后一行打印时,“软件”变量不是YET填充的!我猜Lazy是异步的。所以,它基本上可以工作,但“在某个时候以后”。这很好,但是......当填充软件变量的重要循环实际完成时,我在哪里编写代码?!

根据要求:要播放的数据!

只需创建“something.txt”并写下:

name:
Name 1
.

Option 1:
Value 1
.

Option 2:
Value 2
.

Option 3:
Multi
Line
Value
.

Another_section:
Again
.

Merc的。

1 个答案:

答案 0 :(得分:15)

库返回的Lazy实例为EventEmitters,当“一组”操作完成时,它会发出名为pipe的en事件:

new Lazy(
  ...
).on('pipe', function() {
  // all done
});

修改您的代码以使用此事件导致(唯一的变化接近底部):

fs = require('fs');
Lazy = require('lazy');
path = require('path');

files = fs.readdirSync('.');
var software = {};


files.forEach( function(fileName){


  var m;
  if( m = fileName.match(/^(.*)\.txt$/) ){
    name = m[1];

    console.log("Processing file: " + fileName);
    software[name] = {};
    console.log("Software 1: %j",software);

    var section = 'unset';
    new Lazy(fs.createReadStream(fileName)).lines.forEach(
      function(line){
        var m;
        line = line + '';
        if( m = line.match(/^([a-zA-Z_]*):$/)){
          section = m[1];
          software[name][section] = '';
          console.log("Switching to section " + m[1]);
          console.log("Software 2: %j",software);
        } else if (line == '.'){
          section = 'unset'
        } else if (line == ''){
          section = 'unset'
        } else {
          console.log("LINE: " + line) ;
          software[name][section] = software[name][section] + line + "\n";
          console.log("Software 3: %j",software);
        }
      }

    ).on('pipe', function() {
      console.log("Software 4: %j",software);
    });
  }

});

[编辑]回答有关我如何找到此信息的问题:

我的确检查了the source file for the project;我知道这个库有一个sum方法,可以链接到Lazy实例,最后总结一切; code for that method调用foldrcode for that method收听名为pipeName的事件,该事件默认为in line 22 pipe