有人可以给我一个关于如何将 Promise 与猫鼬一起使用的示例。这是我所拥有的,但它没有按预期工作:
app.use(function (req, res, next) {
res.local('myStuff', myLib.process(req.path, something));
console.log(res.local('myStuff'));
next();
});
然后在 myLib 中,我会有这样的东西:
exports.process = function ( r, callback ) {
var promise = new mongoose.Promise;
if(callback) promise.addBack(callback);
Content.find( {route : r }, function (err, docs) {
promise.resolve.bind(promise)(err, docs);
});
return promise;
};
在某些时候,我希望我的数据会出现,但我怎样才能访问它,或者得到它?