组织承诺代码

时间:2016-04-25 11:18:57

标签: javascript promise bluebird bookshelf.js

我有以下代码。但这看起来类似于回调地狱。如何将此代码重新组织为更合适的承诺方式

   FacilityBooking.forge({
            "booking_id": req.params.id
        })
        .fetch({
            require: true
        })
        .then(function(collection) {
            if(0) { //@todo check if admin
                throw new NotAuthorised(CFG_MESSAGES.error[403]);
            } else {
                Bookshelf.transaction(function(t) {
                    collection
                        .save({
                            "is_valid": 0,
                            "updated_by": req.user.id
                        }, {transacting: t})
                        .tap(function(model) {
                            new FacilityBooking(model.toJSON())
                                 .save({
                                    "is_valid": 1,
                                    "reason_for_reject": req.body.disapprovereason || '' ,
                                    "status": approval_status[req.body.moderation_action]
                                }, {transacting: t})
                                .then(function(collection) {
                                    res.json({
                                        status: true,
                                        message: CFG_MESSAGES.facility_booking.moderate.success
                                    });
                                })
                                .catch(function(err) {
                                    res.json({
                                        status: false,
                                        message: CFG_MESSAGES.facility_booking.moderate.error
                                    });
                                });
                        });
                });
            }
        })
        .catch(function(err) {
            ErrorHandler.handleError(res, err);
        });

3 个答案:

答案 0 :(得分:2)

嗯,你可以把它分成几个函数:

var onErrorProcessingBooking = function(err) {
  ErrorHandler.handleError(res, err);
};

var tap = function(model) {

  var onSuccessSave = function(collection) {
    res.json({
      status: true,
      message: CFG_MESSAGES.facility_booking.moderate.success
    });
  };

  var onErrorSave = function(err) {
    res.json({
      status: false,
      message: CFG_MESSAGES.facility_booking.moderate.error
    });
  };

  new FacilityBooking(model.toJSON())
    .save({
      "is_valid": 1,
      "reason_for_reject": req.body.disapprovereason || '',
      "status": approval_status[req.body.moderation_action]
      }, {    
      transacting: t
    })
    .then(onSuccessSave)
    .catch(onErrorSave);
}

var onSuccessProcessingBooking = function(collection) {
  if (0) {
     throw new NotAuthorised(CFG_MESSAGES.error[403]);
     return;
  }

  Bookshelf.transaction(function(t) {
    collection
      .save({
       "is_valid": 0,
       "updated_by": req.user.id
       }, {
        transacting: t
      })
      .tap(tap);
  });
}


FacilityBooking
  .forge({"booking_id": req.params.id})
  .fetch({require: true})
  .then(onSuccessProcessBooking)
  .catch(onErrorProcessingBooking);

答案 1 :(得分:2)

Promises chain, <div id="game-over"> <h3><font color="orange">Tu as courus <span id="score"></span> mètres.</font></h3> <font color="orange"><h1 id="customegotext">oldHeader</h1></font> <a href="javascript:void(0)" class="button restart">Ressayer ?</a> </div> </div> <script> function myFunction() { var scoretext; if ("score" < 44) { scoretext = "La prochaine fois essaye de dépasser le premier saut"; } else { scoretext = "Caca"; } var element = document.getElementById("oldHeader"); element.innerHTML = "scoretext"; } </script> <script type="text/javascript" src="ljdm.js"></script> 是流控制本身的抽象,你可以从另一个promise中返回一个promise。更好的是,您可以使用协同程序来处理已经包含在书架中的蓝鸟:

then

答案 2 :(得分:0)

我试着回答你的问题,请查看以下代码

Bookshelf.transaction(function(t) {
       FacilityBooking.forge({
               "booking_id": req.params.id
           })
           .fetch({
               require: true
           })
           .then(function(collection) {
               if(0) { //@todo check if admin
                   throw new NotAuthorised(CFG_MESSAGES.error[403]);
               } else {
                   return collection
                       .save({
                           "is_valid": 0,
                           'updated_by': req.user.id
                       }, {
                           transaction: t,
                           patch: true
                       });
               }
           })
           .then(function(model) {
               var data = model.toJSON();
               delete data.id;
               return new FacilityBooking(data)
                       .save({
                           "is_valid": 1,
                           "reason_for_reject": req.body.disapprovereason || 'bbbbbbbbbbb' ,
                           "status": approval_status[req.body.moderation_action]
                       }, {
                           transaction: t,
                           method: 'insert'
                       })
           })
           .then(function(collection) {
               res.json({
                   status: true,
                   message: CFG_MESSAGES.facility_booking.moderate.success
               });
           })
           .catch(function(err) {
               res.json({
                   status: false,
                   message: CFG_MESSAGES.facility_booking.moderate.error
               });
           })
   });