【发布时间】:2014-08-05 02:01:07
【问题描述】:
我正在迁移我的 Mongoose 代码以使用 Promises 来避免末日金字塔。 我想在某一点上打破 Promise 的链条,但我不知道该怎么做。 这是我的代码:
var data = {};
People.find({}).exec()
.then(function(people) {
if (people.length == 0){
// I want to break the chain here, but the console.log() gets executed
res.send('No people');
return;
}
data['people'] = people;
return Events.find({
'city': new mongoose.Types.ObjectId(cityID)
}).lean().exec();
}).then(function(events) {
console.log('here');
data['events'] = events;
res.send(data);
});
【问题讨论】:
-
中断,因为出现错误情况而停止运行?
-
是的,如果第一个查询没有结果,我不想继续下一个查询。
-
见stackoverflow.com/questions/28803287/how-to-break-promise-chain/… 使用
return { then: function() {} };