我有两个来自猫鼬的回调函数,我想用蓝知更鸟的then链接
我的第一个回调函数成功地使用then。
User.findOne().distinct(('Friends.id'), {id: req.body.myId}, {Friends: {$elemMatch: { gender: req.body.gender}}})
.then(function(IDs){
var results = //////some computation
})
.catch(function(error)) {
} 我无法正确地使用语法来链接第二个回调函数,这样它就可以共享第一个回调函数的catch方法。在这种情况下,我不能使用Promise.all,因为第二个回调函数依赖于第一个回调函数的results。无论如何,第二个回调函数如下:
User.find({Friends: { $not: { $elemMatch: { id: req.body.myId }}}, id: {$in: results}}, function(err, users){
})发布于 2016-12-27 00:54:55
你可以像这样把两个承诺串在一起。
User.findOne().distinct(('Friends.id'), {id: req.body.myId}, {Friends: {$elemMatch: { gender: req.body.gender}}})
.then(function(IDs){
var results = //////some computation
// second promise
return User.find({Friends: { $not: { $elemMatch: { id: req.body.myId }}}, id: {$in: results}})
})
.then(function(friends) {
// do something with the result of the second query
})
.catch(function(error)) {
}https://stackoverflow.com/questions/41337699
复制相似问题