Ruční slibování API je nebezpečné, navrhuji něco ve smyslu:
TaskBroker.prototype._connectMongo = Q.nfcall(MongoClient.connect,
'mongodb://127.0.0.1:27017/test',
{});
TaskBroker.prototype.connectMongo = function(){
return this._connectMongo().then(function(db){
console.log("Hello");
// self.stuff...
return 42;
}).catch(function(e){
console.err("connection error",e); // log the connection error, or handler err
throw e; // don't mark as handled, propagate the error.
});
};
Se sliby Bluebird by to vypadalo asi takto:
var MongoClient = Promise.promisifyAll(require("mongodb").MongoClient);
TaskBroker.prototype.connectMongo = function(){
return MongoClient.connectAsync().then(...
// Bluebird will automatically track unhandled errors
};