I am using the below piece of code to execute some set of queries and send the response of doing some validations on query results. For this scenario, I am using async module in Node JS.
async.series([
function(callback){
common.commonValidations(db);
callback();
},
function(callback){
console.log('second function');
res.end(JSON.stringify(gErrors));
callback();
}
], function(err){
console.log('sending res to client');
console.log(err);
});
common.commonValidations(db) function is used to execute few db2 queries.
Here my issue is, though I am using async module, the response is sent to the client while the query execution is going on. As per my understanding of async module, the second function in the array is executed once the first function is done with it's job.
Can someone help me on this? Thanks in advance.
common.commonValidations(db)is also an async function? The thing is, it'sasyncmeaning the first function is executed and completed immediately, it doesn't wait forcommon.commonValidationsto get a response, which is why the response is sent (i.e. the second function) is executed virtually immediately. - Adam