I'm learning node. I have a web application that is interfacing with bitcoind through the bitcoin package, and PostgreSQL through knex. I need to get a bit of data from each module and then pass it all to my view for rendering. My code looks like this so far:
exports.index = function(req, res){
var testnet='';
bitcoin.getInfo(function(e, info){
if(e){ return console.log(e);}
if(info.testnet){
testnet='bitcoin RPC is testnet';
}else{
testnet='nope.. you must be crazy';
}
var c=knex('config').select().then(function(k){
res.render('index', { title: k[0].site_name, testnet: testnet });
});
});
};
The way this is structured though, it will first wait for Bitcoin to reply, and then it will issue the request to PostgreSQL, and then wait a while longer for it to reply. These two wait periods could clearly happen simultaneously. However, I don't know how to do that with promises/callbacks in Javascript. How can I manage this to happen asynchronously, rather than serially?