如何使用 bluebird 链接连续的回调函数,使它们都在 catch 块上

How to use bluebird to chain consecutive callback functions such that they both have on catch block

我有两个来自 mongoose 的回调函数,我想使用 bluebird 的 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){



})

您可以像这样链接两个 promise。

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)) {

    }