aniket mule
aniket mule

Reputation: 91

Perform database operations in asynchronous node.js environment

I want to perform database operations in node app. What I expect is to execute queries one after one i.e. sequentially as second query is dependent on prior operation. Currently, I am using async.series module to achieve the expected results but, when I write a nested query, queries are executed asynchronously. How can I eliminate this asynchronous behavior while executing nested queries? Following is my code.

async.series([
    function(callback){
        dbClient.query(select_show_query,function(err,result1){
            callback(err,result1.rows);
        }) //dbclient query end
    },//function end

    function(callback){
        dbClient.query(select_show_person_query,function(err,result2){
            callback(err,result2.rows);
        }) //dbclient query end
    },//function end
    function(callback){
        dbClient.query(select_show_role_query,function(err,result3){
            callback(err,result3.rows);
        }) //dbclient query end
    },//function end            
    function(callback){
        dbClient.query(select_show_episode_query,function(err,result4){
            callback(err,result4.rows);
        }) //dbclient query end
    },//function end
    function(callback){
        dbClient.query(select_show_genre_query,function(err,result5){
            callback(err,result5.rows);
        }) //dbclient query end
    },//function end    
    function(callback){
        dbClient.query(select_profile_photo_query,function(err,result6){
            callback(err,result6.rows);
        }) //dbclient query end
    }//function end         

],function(err,results){
        if(err){
            res.json({"status": "failed", "message": err.message})
        }
        else{
            res.send(JSON.stringify(results)); 
        }
    } //function end
); //async end

Upvotes: 1

Views: 173

Answers (2)

Davebra
Davebra

Reputation: 488

use async/await of ES2017 (ES8):

async function operations(){

    try {

        let result1 = await dbClient.query(select_show_query);
        let result2 = await dbClient.query(select_show_person_query);
        let result3 = await dbClient.query(select_show_role_query);
        let result4 = await dbClient.query(select_show_episode_query);
        let result5 = await dbClient.query(select_show_genre_query);
        let result6 = await dbClient.query(select_profile_photo_query);

    } catch(err) {
        return res.json({"status": "failed", "message": err})
    }

    // ... do something with result1.rows
    // ... do something with result2.rows
    // ... 

}

Upvotes: 2

arjun trivedi
arjun trivedi

Reputation: 1

you can user async waterfall method.

import waterfall from 'async/waterfall';

Runs the tasks array of functions in series, each passing their results to the next in the array. However, if any of the tasks pass an error to their own callback, the next function is not executed, and the main callback is immediately called with the error.

async.waterfall([
    function(callback) {
        callback(null, 'one', 'two');
    },
    function(arg1, arg2, callback) {
        // arg1 now equals 'one' and arg2 now equals 'two'
        callback(null, 'three');
    },
    function(arg1, callback) {
        // arg1 now equals 'three'
        callback(null, 'done');
    }
], function (err, result) {
    // result now equals 'done'
});

// Or, with named functions:
async.waterfall([
    myFirstFunction,
    mySecondFunction,
    myLastFunction,
], function (err, result) {
    // result now equals 'done'
});
function myFirstFunction(callback) {
    callback(null, 'one', 'two');
}
function mySecondFunction(arg1, arg2, callback) {
    // arg1 now equals 'one' and arg2 now equals 'two'
    callback(null, 'three');
}
function myLastFunction(arg1, callback) {
    // arg1 now equals 'three'
    callback(null, 'done');
}

Source : https://caolan.github.io/async/docs.html

Upvotes: 0

Related Questions