链接上的条件在jquery中延迟 [英] Conditionals on a chained deferred in jquery

查看:80
本文介绍了链接上的条件在jquery中延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我链接 $。延期就像这样。

  $。each(data,function(k,v){
promise.then(function(){
return $ .post(...);
})。then(function (数据){
if(data)... //这里是条件
返回$ .post(...);
})。then(function(data){
if(data)... //这里是另一个条件
返回$ .post(...);
})
});

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

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

  if(data) {
console.log('成功');
}

该代码是否可以介于 .then s?

解决方案

Joey,无论你做得对,取决于你的细节正试图实现。



如果你想用一个终端构建一个长 .then() .done(),其中每个 .then()的'完成'处理程序:




  • 调用异步进程,或

  • 透明地将数据传递到下一个。然后()

  • > 代码应该是以下形式:

      var promise = ...; //返回已解析或可解析的承诺的表达式,让链条开始。 

    $ .each(data,function(k,v){
    promise = promise.then(function(){//`.then()`链由赋值$构建b $ b if(data ...){return $ .post(...);}
    else {return data;} //透明传递`data`
    })。然后(function(data){
    if(data ...){return $ .post(...);}
    else {return data;} //`data` $的透明传递b $ b});
    });

    promise.done(function(){
    console.log(All Done!);
    })。fail(function(jqXHR){
    console .log(不完整 - ajax调用失败);
    });

    但是,如果您尝试执行相同操作,但每个。然后()''完成'处理程序:




    • 调用异步进程,或

    • 中断 .then()



    然后,代码应为以下格式:

      var promise = ...; //一个表达式,它返回已解析或可解析的promise,以启动链。 

    $ .each(data,function(k,v){
    promise = promise.then(function(data){
    if(data ...){return $ .post(...);}
    else {return $ .Deferred()。reject(data).promise();} //强制链被中断
    })。then(function (数据){
    if(data ...){return $ .post(...);}
    else {return $ .Deferred()。reject(data).promise();} //强制链中断
    });
    });

    promise.done(function(){
    console.log(All Done!);
    })。fail(function(obj){//注意:` obj`可以是数据对象或jqXHR对象,具体取决于导致拒绝的原因。
    console.log(未完成 - ajax调用失败或返回的数据确定then()链应该被中断);
    });


    Let say I chained the $.Deferred like this.

    $.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!");
    });
    

    Am I doing it right? how do I prevent the next chain to execute if the condition return false, and where do I do this:

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

    Can that code be in between those .thens?

    解决方案

    Joey, whether or not you are doing it right depends on the detail of what you are trying to achieve.

    If you are trying to build one long .then() chain with a terminal .done(), where each .then()'s 'done' handler either :

    • calls an asynchronous process, or
    • transparently passes data on to the next .then() in the chain

    then, the code should be of the following form :

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

    If, however, you are trying to do the same but where each .then()'s 'done' handler either :

    • calls an asynchronous process, or
    • interrupts the .then() chain

    then, the code should be of the following form :

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

    这篇关于链接上的条件在jquery中延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆