【问题标题】:Conditionals on a chained deferred in jqueryjquery中链式延迟的条件
【发布时间】:2013-05-03 03:21:50
【问题描述】:

假设我像这样链接了$.Deferred

$.each(data, function(k, v) {
    promise.then(function() {
        return $.post(...);
    }).then(function(data) {
        if(data)... // here is the conditions
        return $.post(...);
    }).then(function(data) {
        if(data)... // here is another condition
        return $.post(...);
    })
});

promise.done(function() {
    console.log("All Done!");
});

我做得对吗?如果条件返回 false,我如何防止下一个链执行,我在哪里执行此操作:

if(data){
   console.log('Success');
}

该代码可以在.thens 之间吗?

【问题讨论】:

    标签: javascript jquery ajax chaining deferred


    【解决方案1】:

    乔伊,你做对与否取决于你想要达到的目标的细节。

    如果您尝试使用终端 .done() 构建一个长的 .then() 链,其中每个 .then() 的“完成”处理程序之一:

    • 调用异步进程,或
    • 透明地将数据传递到链中的下一个.then()

    那么,代码格式如下:

    var promise = ...;//An expression that returns a resolved or resolvable promise, to get the chain started.
    
    $.each(data, function(k, v) {
        promise = promise.then(function() {//The `.then()` chain is built by assignment 
            if(data...) { return $.post(...); }
            else { return data; }//Transparent pass-through of `data`
        }).then(function(data) {
            if(data...) { return $.post(...); }
            else { return data; }//Transparent pass-through of `data`
        });
    });
    
    promise.done(function() {
        console.log("All Done!");
    }).fail(function(jqXHR) {
        console.log("Incomplete - an ajax call failed");
    });    
    

    但是,如果您尝试做同样的事情,但每个 .then() 的“完成”处理程序要么:

    • 调用异步进程,或
    • 中断.then()

    那么,代码格式如下:

    var promise = ...;//An expression that returns a resolved or resolvable promise, to get the chain started.
    
    $.each(data, function(k, v) {
        promise = promise.then(function(data) {
            if(data...) { return $.post(...); }
            else { return $.Deferred().reject(data).promise(); }//Force the chain to be interrupted
        }).then(function(data) {
            if(data...) { return $.post(...); }
            else { return $.Deferred().reject(data).promise(); }//Force the chain to be interrupted
        });
    });
    
    promise.done(function() {
        console.log("All Done!");
    }).fail(function(obj) {//Note: `obj` may be a data object or an jqXHR object depending on what caused rejection.
        console.log("Incomplete - an ajax call failed or returned data determined that the then() chain should be interrupted");
    });
    

    【讨论】:

      【解决方案2】:

      jQuery's then 返回一个新的 Promise,由以下链式 then 监控。从前一个then 返回的任何内容都作为下一个then 的第一个参数传递。

      promise.then(function() {
        return $.post(...);
      }).then(function(data) {
        //we return false or some indicator that next shouldn't run
        if(!data) return false; 
        //else we return something
        else return $.post(...);
      }).then(function(data) {
        //here we receive false, we return early, preventing further code from executing
        if(!data) return false;
        //otherwise, the following code runs
        return $.post(...);
      })
      

      【讨论】:

      • 在返回 false 之前我可以登录吗?
      • @JoeySalacHipolito if(data) 在哪里?
      • 在所有条件下,例如如果第一个然后返回false,我想console.log()链的每个状态...
      • @JoeySalacHipolito 让您知道,返回 $.post 会返回延迟。您所有的then 检查data 这将始终为真,因为您在所有thens 上返回一个延迟对象。
      • 嗯,好的,我试试看。
      猜你喜欢
      • 2014-11-06
      • 2017-03-17
      • 2015-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-04
      • 2016-12-08
      • 1970-01-01
      相关资源
      最近更新 更多