对于需要上一次迭代结果的节点j中的LOOP

时间:2017-08-21 10:32:14

标签: node.js for-loop asynchronous

我需要在Node JS中进行循环,我需要上一次迭代的结果。

function jsonParser(txt_file, cb) {
var perc = 0;

lines = txt_file.split('\n');

insert_communication = 'INSERT INTO communication (account_id, contact_id, type_com_id, coin_id, com_datetime, ' +
    'destination_key, destination_st, duration, cost, description, discount)  VALUES ';

insert_internet = 'INSERT INTO internet_data (account_id, contact_id, type_com_id, coin_id, com_datetime, ' +
    'duration, cost, description, discount)  VALUES ';

for (let i = 2; i < lines.length; i++) {

    readTextFile2(lines[i], function (cb) {
        if (cb[0] == "communication")
            insert_communication += cb[1] + ',';
        if (cb[0] == "internet")
            insert_internet += cb[1] + ',';


    });        
}

cb(insert_communication);

}

我应该如何实现这一目标?

由于

1 个答案:

答案 0 :(得分:0)

  for (let i = 2; i < lines.length; i++) {

        const previous = lines[i - 1 ];
        const hasPrevious = !!previous;

        if(hasPrevious) {
             //ok you have a previously value, now you could use it

        }

        readTextFile2(lines[i], function (cb) {
              if (cb[0] == "communication")
                    insert_communication += cb[1] + ',';
              if (cb[0] == "internet")
                    insert_internet += cb[1] + ',';


        });
  }