Fetching data through a function.
Establishing a connection and retrieving necessary information. Some code has been omitted for brevity.
function executeSQL(sql, bindParams , options) {
return new Promise(function(resolve, reject) {
...
resolve(result);
});
}
Utilizing the function in a controller
exports.index = function(req, res){
database.executeSQL('SELECT 1 FROM DUAL', [] , {})
.then(function(result) {
res.render('index' , { TITLE : 'Lorem Ipsum Blog' });
})
.catch(function(err) {
next(err);
});
};
The index
controller is linked to the corresponding route.
I plan on invoking the executeSQL function twice. Only after both tasks are completed do I intend to call res.render
to display the fetched data.
How should I sequence these calls? Is chaining them necessary or can I handle them asynchronously, waiting until both are finished before rendering?