Promise在传递给下一个函数之前不会等待

时间:2017-05-21 06:36:10

标签: javascript node.js promise

我试图在解析它们之后将所有事务插入到数据库中。 我有一个名为load.newload(transactionlist)的函数;在婴儿解析完成解析后传递。

我已经测试了我的代码,如果文件列表很短,它会加载所有文件,但是一旦它变长(大约10个或更多文件),它就会在完成之前调用load.newload(transactionlist)函数解析。

我想也许我应该对baby.parse功能有另一个承诺,但它似乎没有用。

结果如下:

解析file1, 解析文件2, 解析文件3, SQL连接, 插入数据成功, 解析文件4, 解析file5

我该如何解决这个问题?

这是我的代码:

var baby = require('babyparse');
var read = require('read-file');
var Promise = require('promise');
var load = require('../sql/insert');
var transactionlist = [];
var incomingfile = function(file){
     //async load file and then parse after loading
        var promise = new Promise(function (resolve, reject) {
              read( file, 'utf8', function (err, res){
              if (err) 
                {reject(err);
                console.log('Oops, an error occured while parsing the data: %s', err);}
              else {resolve(res);
                    console.log(file);}
            });
        }).then(function (results){
                var promise = new Promise(function (resolve, reject) {
                baby.parse(results , {
                    dynamicTyping: true,
                    delimiter: "|",
                    complete: function (results, error) {
                        if(error)
                        {console.log('Something went wrong');
                         reject(error);}
                        else {
                              for(i = 1; i < results.data.length - 1; i++)
                                {
                                    var transaction  = {
                                        column1 : results.data[i][14].substring(0, 6),
                                        column2: results.data[i][2].split(' ').join(''),
                                        column3: results.data[i][14].split(' ').join(''),
                                        column4: results.data[i][8],
                                        column5: results.data[i][9]
                                    }
                                    transactionlist.push(transaction);
                                }//end for loop
                                resolve(results);
                                }
                        //console.log("Finished:", JSON.stringify(results.data));
                    }//end baby parse complete:function
                    });//end baby parse function
                }).then(function (results){
                    load.newload(transactionlist);
            });//end inner .then function
        });//end .then function
}//end incoming file function


exports.incomingfile = incomingfile;


//newload function in insert.js
const sql = require('mssql/msnodesqlv8');
var config = require('../connect');
var newload = function (transactionlist){
            sql.connect(config)
              .then(() => {
                console.log('connected');
                    const table = new sql.Table('settlement');
                    table.create = true;
                    //table.columns.add('transactionid', sql.Int, { nullable: false, primary: true});
                    table.columns.add('colum1', sql.VarChar(6), { nullable: false });
                    table.columns.add('column2', sql.VarChar(50), { nullable: false });
                    table.columns.add('column3', sql.VarChar(50), { nullable: false });
                    table.columns.add('column4', sql.VarChar(50), { nullable: false });
                    table.columns.add('column5', sql.VarChar(5), { nullable: false });

                     //add here rows to insert into the table
                    for (i = 0; i < transactionlist.length; i++) {

                        table.rows.add(transactionlist[i].column1, transactionlist[i].column2, transactionlist[i].column3, transactionlist[i].column4, transactionlist[i].column5);
                        console.log('transaction list', i , JSON.stringify(transactionlist.length));
                      }     
                const request = new sql.Request();
                console.log('Ready to insert data');
                request.bulk(table, function (error, results){
                    if(error)
                    {
                        console.log('Something went wrong while inserting the data into the database');
                        sql.close();
                    }
                    else {
                        console.log('Data has been inserted successfully');
                        sql.close();
                    }
                })  
              });
};

exports.newload = newload;

1 个答案:

答案 0 :(得分:0)

问题可能是由于使用全局变量transactionlist在promises之间共享数据。鉴于它是一个链式承诺的情况,处理数据transactionlist的更好方法可能是将它与return语句一起传递,让下一个承诺处理它而不会产生歧义(它是否已更新)。

这就是我做链接承诺的方式。

var baby = require('babyparse');
var read = require('read-file');
var Promise = require('promise');
var load = require('../sql/insert');
// var transactionlist = [];
var incomingfile = function(file){
     //async load file and then parse after loading
        var promise = new Promise(function (resolve, reject) {
              read( file, 'utf8', function (err, res){
              if (err) 
                {
                    reject(err);
                    console.log('Oops, an error occured while parsing the data: %s', err);
                }
              else {
                    resolve(res);
                    console.log(file);
               }
            });
        }).then(function (results){
                var promise = new Promise(function (resolve, reject) {
                baby.parse(results , {
                    dynamicTyping: true,
                    delimiter: "|",
                    complete: function (results, error) {
                        if(error)
                        {
                            console.log('Something went wrong');
                            reject(error);
                        }
                        else {
// define a local var here 
var transactionlist = [];

                                    for(i = 1; i < results.data.length - 1; i++)
                                {
                                    var transaction  = {
                                        column1 : results.data[i][14].substring(0, 6),
                                        column2: results.data[i][2].split(' ').join(''),
                                        column3: results.data[i][14].split(' ').join(''),
                                        column4: results.data[i][8],
                                        column5: results.data[i][9]
                                    }
                                transactionlist.push(transaction);

                                }//end for loop
// return transactionlist instead of updating global var
                                resolve([results, transactionlist]);
                                }
                        //console.log("Finished:", JSON.stringify(results.data));
                    }//end baby parse complete:function
                    });//end baby parse function
                }).then(function (results){
// load transactionlist again from passed values
var transactionlist = results[1];
results = results[0];
                    load.newload(transactionlist);
            });//end inner .then function
        });//end .then function
}//end incoming file function

我希望你能看到我所做的改变(它们没有正确缩进和评论)。使用此方法,您在使用回调/承诺时绝不会错过数据。