首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用蓝鸟链接连续回调函数,使它们都具有on catch块

如何使用蓝鸟链接连续回调函数,使它们都具有on catch块
EN

Stack Overflow用户
提问于 2016-12-27 00:45:38
回答 1查看 54关注 0票数 0

我有两个来自猫鼬的回调函数,我想用蓝知更鸟的then链接

我的第一个回调函数成功地使用then

代码语言:javascript
运行
复制
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。无论如何,第二个回调函数如下:

代码语言:javascript
运行
复制
  User.find({Friends: { $not: { $elemMatch: { id: req.body.myId }}}, id: {$in: results}}, function(err, users){



})
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-12-27 00:54:55

你可以像这样把两个承诺串在一起。

代码语言:javascript
运行
复制
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)) {

    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41337699

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档