lundi 17 août 2015

Questions from the synchronous node.js

I want a synchronous results like this

100-elements
commit
100-elements
commit
100-elements
commit
100-elements
commit

However, the code that comes out these results.

100-elements
100-elements
100-elements
100-elements
100-elements
.
.
.
.
100-elements
commit
commit
commit
commit
commit
commit
commit
.
.
.
.
commit

How can I see the results I want?

mysql.getConnection(function(err, connection){

  if(err){ connection.release(); return; }

  var array = [];
  var count = 300000;
  var length = count;

  for(var i=0; i<count; i++){

    (function(currentI, master){
      process.nextTick(function(){

        array.push([currentI, master]);

        if(currentI % 5000 === 0){
          console.log('100-elements');
          array = [];

          connection.beginTransaction(function(err){
            if(err){
              throw err;
            }

            var query = "insert into users(username, password) values ?";

            connection.query(query, [array], function (err, rows) {

              if (!err) {
                //commit start
                connection.commit(function(){
                  if (err) {
                    console.error(err);
                    connection.rollback(function () {
                      console.error('rollback error');
                      throw err;
                    });
                  } else {
                    console.log("Commit");
                  }/ / if err
                    }); //commit end
                } else {
                                  console.log(err);
                connection.rollback(function(){
                  throw err;
                });
              } // if
            }) // cnnection query
          }); // beginTransaction
        } //if

      });
    }(i, 'master'));
  } //for
}) // getConnection




Aucun commentaire:

Enregistrer un commentaire