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 .then
s?
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 :.then()
in the chainthen, the code should be of the following form :
If, however, you are trying to do the same but where each
.then()
's 'done' handler either :.then()
chainthen, the code should be of the following form :
jQuery's
then
returns a new promise, which is monitored by the following chainedthen
. Whatever is returned from the previousthen
is passed as the first argument of the nextthen
.