Edit - please see notes at the bottom on why I don't believe this is a duplicate question
Using jquery 3.2.1 and Bootstrap 3.3.7
Part of my application has the following flow:
A user clicks on an anchor inside a modal window with an ID of
#notifierModal
This makes the first ajax request which writes to a database storing preferences on choices a user makes (each choice is an anchor from step 1 - clicking toggles the preference on/off). The response from this ajax request is JSON which is in the following format:
{result: "success", message: "Notification preference has been updated"}
If there was an error - in terms of making a database update - the JSON is
of a similar structure but would have {result: "error"}
and an appropriate message.
A second ajax request is made to update the contents of the modal (
#notifierModal
). This is effectively to "refresh" the data shown in the modal following updating preferences in a database from step 2.The result (success or error message) given by the response from step 2 is updated inside
#notifierModal
.
My question - I have implemented the above with the following ajax:
// Step 1
$('#notifierModal .modal-body').on('click', '.toggle-notifier a', function(e) {
e.preventDefault();
// Step 2
$.ajax({
url: $(this).attr('href'),
method: 'get'
}).then(function(response2) {
// Step 3
$.ajax({
url: '/notifier-modal',
method: 'get'
}).then(function(response3) {
$('#notifierModal .modal-body').html(response3);
}).done(function() {
// Step 4
if (response2.result == 'error') {
$('#notifierModal .modal-body .outcome').html(response2.message);
}
if (response2.result == 'success') {
$('#notifierModal .modal-body .outcome').html(response2.message);
}
});
});
});
However, if I replace both instances of .then()
with .done()
it works in exactly the same way.
I am trying to ensure that the first ajax request (step 2) completes before continuing to making the second ajax request (step 3). I have read about Promises and the information on How do I chain three asynchronous calls using jQuery promises?
I don't understand why using .done()
gives the same outcome and which is the correct approach? I appreciate there may be other improvements I can make to the js but my question here is concerning the difference between using .done()
on it's own versus a .then()/.done()
approach?
My application is "working" - in the sense it gives the same results from both ways - but I feel like I'm making a mistake because I don't understand the difference between these approaches. Please can someone clarify this?
Not a duplicate: Someone suggested this is a duplicate of jQuery deferreds and promises - .then() vs .done(). I don't think it is because it says:
There is also difference in way that return results are processed (its called chaining,
done
doesn't chain whilethen
produces call chains)
I've updated the variables in callbacks to response2
and response3
to show which step they are in within the js. However, I can use response2
(from step 2) inside step 4 irrespective of whether I use .done()
or .then()
. How is that possible? The outcome is what I want, but I don't understand how it's working, which is concerning.