程序go 2017-02-08 14:53 采纳率: 100%
浏览 26

AJAX-具有嵌套承诺

var preadd = $.ajax({
  type: 'POST',
  url: url,
  data: {
    some data
  },
  success: function(output) {

    if (output != 12 && output != 13) {

      some code here...

      var arr = [];
      arr.push.apply(arr, val.split(","));
      arr.splice(-1, 1);
      for (var i = 0; i < arr.length; i++) {
        arr[i] = arr[i].trim();
      }
      chk dup1 is another ajax
      function outside
      var chk = chkdup1();
      console.log(chk);
      chk.then(function() {
        if (result == 13) {

          rem = confirm("Duplicate data will be removed, are you sure!");
          if (rem == false) {
            return false;
          }
          if (rem == true) {
            remdens1();
          }
        }
      });
    }
    if (output == 13) {
      rem = confirm("Duplicate data will be removed, are you sure!");
      if (rem == true) {
        remdens1();
      }
    }

    if (output == 12) {
      $('#valensdata').html("success").css('color', 'green');
    }
    return rem;
  }
});

preadd.then(function() {
  console.log(rem);
  if (rem == false) {
    return false;
  }

  fnensadd();

});
  1. even if i press cancel in confirm inside .then function of preadd,
  2. i am unable to get the value stored inside rem variable.
  3. preadd.then at the bottom function is executing even when return false.

can anyone kindly guide me.

  • 写回答

1条回答 默认 最新

  • weixin_33734785 2017-02-08 15:15
    关注

    even if i press cancel in confirm inside .then function of preadd:

         Promise Preadd has no knowledge of cancel. You are returning a value false when cancel is clicked.     
    

    i am unable to get the value stored inside rem variable.

     Assuming rem is a local variable of success function, the variable dies when success function is completed. A bad way of coding would be creating rem in  larger scope , eg sibling of preadd
    

    preadd.then at the bottom function is executing even when return false.

    resolveCB of preadd.then( resolveCB, rejectCB) will always be called unless an until there is some error or promise is rejected. Returning false is not same as rejecting promise.   
    

    After saying this ,either use "then" or "success". If you want to Prevent CB Hell, always use promise.

    A better code structure with promises would be as follows

    var preadd = $.ajax({
       type: 'POST',
       url: url,
       data: {
         some data
      }
    
    preadd.then(function() {
         // Success of PreAdd
              return chkdup1() 
    }).then(function(){
        // Success of chkdup1
        //by some internal logic build rem
        if(rem)
        return Promise.resolve(rem )
        return Promise.reject(rem)
    }).then(function(data){
    
         // rem was true
          fnensadd();
    },function(error){
        // rem was false
    
     });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错