手动散布API是很危险的,我建议采取以下措施:
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. });};
有了Bluebird的承诺,它看起来就像:
var MongoClient = Promise.promisifyAll(require("mongodb").MongoClient);TaskBroker.prototype.connectMongo = function(){ return MongoClient.connectAsync().then(... // Bluebird will automatically track unhandled errors };
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)