Search code examples
javascriptjqueryajaxchainingdeferred

Conditionals on a chained deferred in jquery


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?


Solution

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