Lazyexpert
Lazyexpert

Reputation: 3154

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.

Upvotes: 2

Views: 585

Answers (1)

Robert Rossmann
Robert Rossmann

Reputation: 12131

Your async.parallel definition is wrong - mainly, your callback that should be called when the parallel functions have finished is called immediately:

async.parallel([
    // Functions...
], whendone(null) /* <-- you are calling it right away! */)

Instead, you must pass the callback function as an argument without calling it:

async.parallel([
    // Functions...
], whendone /* No invocation */)

The purpose of the callback is that async may call it once it determines that all the parallel functions have completed. Your original code invoked the callback right away, and async instead received the return value of that callback as a callback, not the actual function.

Upvotes: 2

Related Questions