Manually promisifying an API is dangerous, I suggest something along the lines of:
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.
});
};
With Bluebird promises, that'd look something like:
var MongoClient = Promise.promisifyAll(require("mongodb").MongoClient);
TaskBroker.prototype.connectMongo = function(){
return MongoClient.connectAsync().then(...
// Bluebird will automatically track unhandled errors
};
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…