I need to do a few independent database queries in Node.js. After all queries are executed, response should be sent. My first try looks like this:
templateData = {};
model.getA(function(result) {
templateData.A = result;
model.getB(function(result) {
templateData.B = result;
model.getC(function(result) {
templateData.C = result;
response.send('template', templateData);
})
})
});
Of course, this approach in Node.js is not good at all, because all functions are called sequentially and I'm loosing advantages of asynchronous programming pattern. I'm new to Node.js and it's still unclear to me how to call getA()
, getB()
and getC()
in parallel and send response just after everything is finished. Is there some really simple and common way to achieve this?