【问题标题】:node.js chain multiple promises (with mongoose)node.js 链多个承诺(与猫鼬)
【发布时间】:2016-03-10 10:04:44
【问题描述】:

以下是我正在处理的一个典型的 Promise 函数。

var _delete = function(t, id) { 
  return Promise.cast(Event.find({where: {id: id}}, {transaction: t}))
  .then(function(d){
    if (d) {
      // ------- (*)
      return Promise.cast(d.updateAttributes({status: -1}, {transaction: t}))
      .then(function(){
          // do inventory stuff 
          return Promise.cast(Inventory.update({}).exec())
          .then(function(d){
               // do something 
          })
      }).then(function(){
          // do product stuff
          return Promise.cast(Product.update({}).exec())
          .then(function(d){
               // do something 
          })
      })
    } else {
      return Promise.reject('this transaction list does not exist');
    }
  });
};

这看起来没问题,直到我处理更复杂的更新/创建代码会变得非常混乱。

目前我正在做的承诺是 1.我有很多无用的return true语句,唯一的目的是去下一个.then语句 2. promise 以嵌套的方式编程。输入参数通常很复杂并且有超过 1 个参数,所以我不能做这样的事情

.then(fun1).then(fun2)

...等

这使我无法通过'tap' .then 语句启用/禁用功能。

所以我的问题是如何正确执行此操作?谢谢..


以下是我所说的真正丑陋的事情......

var _process = function(t, tid) {
  var that = this;
  return Promise.cast(Usermain.find({where: {transaction_id: tid}}))
  .bind({})  // --- (*)
  .then(function(d){
    this.tmain = d;
    return true;   // ---- do nothing, just go to next thennable (is this correct)
  }).then(function(){
    return Promise.cast(Userlist.findAndCountAll({where: {transaction_id: tid}}))
  }).then(function(d){
    this.tlist = d;
    return true;  // ---- do nothing, just go to next thennable (is this correct)
  }).then(function(){
    if (this.tmain.is_processed) {
      return Promise.reject('something is wrong');
    }
    if (this.tlist.count !== this.tmain.num_of_tran) {
      return Promise.reject('wrong');
    }
    return Promise.resolve(JSON.parse(JSON.stringify(this.tlist.rows)))
    .map(function(d){
      if (d.is_processed) return Promise.reject('something is wrong with tran list');
      return true;  // goto next then
    });
  }).then(function(){
    return Promise.cast(this.tmain.updateAttributes({is_processed: 1}, {transaction: t}));
  }).then(function(){
    return Promise.resolve(this.tlist.rows)
    .map(function(d){
      var tranlist = JSON.parse(JSON.stringify(d));
      return Promise.cast(d.updateAttributes({is_processed: 1, date_processed: Date.now()}, {transaction: t}))
      .then(function(d){
        if (!d) {
          return Promise.reject('cannot update tran main somehow');
        } else {
            if (tranlist.amount < 0) {
              return Usermoney._payBalance(t, tranlist.user_id, -tranlist.amount);
            } else {
              return Usermoney._receiveBalance(t, tranlist.user_id, tranlist.amount);
            }
          }
      });
    });
  });
}

【问题讨论】:

  • 为什么需要那些JSON.parse(JSON.stringify(…))s?
  • 我认为当数组对象是 BSON 时 lodash 会做一些意想不到的事情。此外,我使用的 ORM 是 sequelize,我找不到将返回数据类型转换为 JSON 的内在方法。
  • 我认为 mongoose 实例有一个 .toJSON 方法可以做到这一点(这就是 JSON.stringify 首先起作用的原因)。
  • 有帮助。现在会记住这一点,因为代码仍然完全在 ES5 中..

标签: javascript node.js promise bluebird


【解决方案1】:

你可以做两件事:

  • Unnest then callbacks
  • 模块化。这些“做产品”和“做库存”的事情可能会成为它们自己的功能(甚至是相同的功能?)。

在这种情况下,取消嵌套可以执行以下操作(假设您不需要在注释部分中使用闭包):

function _delete(t, id) { 
    return Promise.cast(Event.find({where: {id: id}}, {transaction: t}))
    .then(function(d){
        if (d) {
            return Promise.cast(d.updateAttributes({status: -1}, {transaction: t}));
        else
            throw new Error('this transaction list does not exist');
    })
    .then(function(){
        // do inventory stuff 
        return Promise.cast(Inventory.update({}).exec())
    })
    .then(function(d){
        // do something 
    })
    .then(function(){
        // do product stuff
        return Promise.cast(Product.update({}).exec())
    })
    .then(function(d){
        // do something 
    });
}

【讨论】:

  • 对不起,我的情况有点复杂。我将在上面解释更多细节。
  • 谢谢我在处理所有以后的查询/更新之前最终会这样做。 (并将中间结果存储在this
【解决方案2】:

在我的项目中,我使用Async.js

我认为您需要将 _process 方法分解为小动作

  1. 依赖于先前操作结果的操作 - 此处可能使用异步 waterfall 模式
  2. 不依赖于先前动作结果的动作,它们可以并行执行
  3. 使用一些自定义流程

这是我的应用程序中的一个示例:

async.waterfall([

    function findUser(next) {
        Users.findById(userId, function (err, user){
            if(err) {
                next(new Error(util.format('User [%s] was not found.', userId)));
                return;
            }

            next(null, user);
        });
    },

    function findUserStoriesAndSurveys(user, next) {

        async.parallel([
            function findStories(callback) {
                // find all user stories

                Stories.find({ UserGroups: { $in : user.Groups } })
                    .populate('Topic')
                    .populate('Episodes')
                    .exec(function(err, stories) {
                        if(err) {
                            callback(err);
                            return;
                        }

                        callback(null, stories);
                    });
            },
            function findSurveys(callback) {
                // find all completed surveys

                Surveys.find({
                    User: user
                }).exec(function(err, surveys) {
                    if(err) {
                        callback(err);
                        return;
                    }

                    callback(null, surveys);
                });
            }
        ],
        function(err, results) {
            if(err) {
                next(err);
                return;
            }

            next(null, results[0], results[1]);
        });
    },

    function calculateResult(stories, surveys, next) {

        // do sth with stories and surveys

        next(null, { /* result object */ });
    }

], function (err, resultObject) {
    if (err) {
        res.render('error_template', {
            status: 500,
            message: 'Oops! Server error! Please reload the page.'
        });
    }

    res.send(/* .... */);
});

自定义流程请参考 Async 文档,它确实包含很多常见模式,我也在客户端 JavaScript 中使用了这个库。

【讨论】:

  • 我的理解是 promise 和 async.js 可以达到同样的效果,但是更多的人正在转向 promise 风格的编程(如果我错了,请纠正我)。问题是对于嵌套的 Promise,代码仍然变得非常混乱......
  • 拜托,你能用async写OP的具体代码,而不是只提供一些示例代码吗?
  • @ChandlerLee 我用我的项目中的 sn-p 更新了答案,希望这会有所帮助。
猜你喜欢
  • 2019-06-04
  • 2018-02-15
  • 2015-05-12
  • 2018-09-01
  • 1970-01-01
  • 2012-12-14
  • 2017-06-15
  • 2017-08-18
  • 1970-01-01
相关资源
最近更新 更多