jeudi 16 avril 2015

Issue with async.parallel

The problem, i belive, is somewhere aroung the async flow and incorrect parallel work for me...


So, i have two functions. The first function is updating the database variables (host, name, login, pass), it gets these values from the leveldb. Here's the code:



function refreshDbValues (whendone) {
async.parallel([
function (callback) {
db.get('dbhost', function (err, value) {
if(err) callback(err);
dbhost = value;
console.log('host ' + dbhost);
callback(null);
});
},
function (callback) {
db.get('dbname', function (err, value) {
if(err) callback(err);
dbname = value;
callback(null);
});
},
function (callback) {
db.get('dblogin', function (err, value) {
if(err) callback(err);
dblogin = value;
console.log('user ' + dblogin);
callback(null);
});
},
function (callback) {
db.get('dbpass', function (err, value) {
if(err) callback(err);
dbpass = value;
callback(null);
});
},

], whendone(null));
}


And the second function is serving mysql queries from router. And it has to execute the main code after the first function is complete. The code:



function handle_database(query, callback) {

refreshDbValues(function (err) {
if(err) callback(err);
console.log('just for test');

var pool = mysql.createPool({
connectionLimit : 100, //important
host : dbhost,
user : dblogin,
password : dbpass,
database : dbname,
debug : false,
multipleStatements: true
});

pool.getConnection(function(err,connection){
if (err) {
if(typeof(connection) != 'undefined')
connection.release();
return callback(err);
}

connection.query(query,function (err, rows){
connection.release();
if(!err) return callback(null, rows);
else console.log(err);
});

connection.on('error', function(err) {
return callback("Error in connection database");
});
});
});
}


My output is as follows:



just for test
host ***
user ***


And i was awaiting 'just for test' to come last. Am i wrong in my logic, or something wrong with code? Regards for any help.


Aucun commentaire:

Enregistrer un commentaire