Saturday 20 May 2017

Promise doesn't wait before passing to next function

I am trying to insert all transactions into a database after I've parsed them. I have a function called load.newload(transactionlist); which is passed after baby parse finishes parsing.

I've tested my code, and it loads all the files if the file list is short, but once it gets long (around 10 files or more), it calls the load.newload(transactionlist) function even before it finished parsing.

I was thinking maybe I should have another promise for the baby.parse function, but it doesn't seem to work.

Results look like this:

parse file1, parse file2, parse file3, SQL connected, Insert data successful, parse file4, parse file5

How can I fix this?

This is my code:

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;



via Joe

No comments:

Post a Comment