Assuming a pseudo-code like this:
function get_data_and_reply(response) {
// process 1:
db_connect.query(..., function(...){
// get A from db
});
// process 2:
db_connect.query(..., function(...){
// get B from db
});
// process 3: aggregate A + B and reply
}
A possible solution is using callback (hell ;) ), like this
function get_data_and_reply(response) {
// process 1:
db_connect.query(..., function(...){
// get A from db
if (err) {} else {
// process 2:
db_connect.query(..., function(...){
// get B from db
if (err) {} else {
// process 3: aggregate A + B and reply
}
});
}
});
}
but I would like to maintain the parallelism with processes 1 and 2 (can take time, and then would be better if they go in parallel) and then "serialise" with process 3.
How can I synchronise N steps (in parallel) with one?
I have thought about playing with variables and looping until the other method is ended, but sounds a little intricate, or not?
function get_data_and_reply(response) {
// process 1:
db_connect.query(..., function(...){
// get A from db
});
// process 2:
db_connect.query(..., function(...){
// get B from db
});
// process 3: aggregate A + B and reply
while (A_is_not_ready && B_is_not_ready) {}
}
If you do not mind using libraries, async is an awesome one. You would use it like this:
function get_data_and_reply(response) {
async.parallel({
A: function (next) {
// process 1:
db_connect.query(..., next); // Assuming the db returns the result like (err, result)
},
B: function (next) {
// process 2:
db_connect.query(..., next);
}
}, function (err, results) {
// This is called when both parallel functions are done. Use results.A and .B
// process 3: aggregate A + B and reply
})
}
To do it without any helper is also pretty simple, thought not as pretty:
function get_data_and_reply(response) {
var aResult, bResult;
// process 1:
db_connect.query(..., function(..., result){
// get A from db
aResult = result;
done();
});
// process 2:
db_connect.query(..., function(..., result){
// get B from db
bResult = result;
done();
});
function done() {
if(aResult && bResult) {
// process 3: aggregate A + B and reply
}
}
}
Don't forget to handle errors should you use the latter way.
Read up on promises pattern, and specially the Q library for node: http://strongloop.com/strongblog/promises-in-node-js-with-q-an-alternative-to-callbacks/