我正在迁移我的猫鼬代码,以使用承诺,以避免厄运金字塔。我想在某一点上打破诺言的枷锁,但我不知道怎么做。这是我的密码:
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);
});发布于 2014-08-04 14:07:03
您需要在处理程序中拒绝或抛出错误,以“停止”承诺链的运行。
在猫鼬博士中,您可能想要调用诺言的#reject方法。
您为reject提供的处理程序应该检查原因和“做正确的事情”(比如,如果您正在执行RESTful操作,则返回404或空数组)。
如果您无法访问这个承诺(例如,您已经在处理程序中),只需使用throw new Error()即可。
这将调用您提供的拒绝处理程序。
https://stackoverflow.com/questions/25118554
复制相似问题