You just did. There are 3 things that can happen in a jQuery then
:
If you don't return anything at all to then
, the next chained then will resolve with the same value as the previously attached then
.
$.ajax(...)
.then(function(result){ // result = 5
// This block executes when the first AJAX resolves
// Do nothing
})
.then(function(result){ // result = 5
// This block executes right after the previous `then`
// and still uses the same resolved value
});
If you return a promise (like from a jQuery ajax
or Deferred
), the next chained then
will resolve when that returned promise resolves.
$.ajax(...)
.then(function(firstAjaxResult){
// This block executes when the first AJAX resolves
// Return a promise
return $.ajax(...);
})
.then(function(secondAjaxResult){
// This will resolve when the second AJAX resolves
});
If you return anything other than a promise, the next chained then will resolve with the value returned by the previous then
instead of the original value.
$.ajax(...)
.then(function(result){ // result = 5
// This block executes when the first AJAX resolves
// Return a modified result
return result * 3;
})
.then(function(newResult){ // newResult = 15
// This block executes right after the previous `then`
// but resolves with the modified value
});