节点.js链接多个承诺(与猫鼬)

node.js chain multiple promises (with mongoose)

本文关键字:承诺 js 链接 节点      更新时间:2023-09-26

以下是我正在处理的典型 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.我有很多无用的返回true语句,唯一的目的是转到下一个.then语句2. 承诺以嵌套样式编程。此外,输入参数通常很复杂,并且有超过 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);
            }
          }
      });
    });
  });
}

你可以做两件事:

  • 取消嵌套then回调
  • 模块 化。这些"做产品的东西"和"做库存的东西"可能会成为它们自己的功能(甚至相同?

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

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 
    });
}

在我的项目中,我使用异步.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(/* .... */);
});

请参考 异步文档 对于自定义过程,它确实包含很多常见模式,我也在我的客户端 JavaScript 中使用此库。