Panagiotis Koursaris Panagiotis Koursaris - 4 months ago 10
jQuery Question

AJAX done() executed when finished all of request staff

I have an AJAX request to let the user delete some data from the list. When the ajax done I call another ajax request so the list updated with new the list of data. AJAX request to delete data:

$.ajax({
method: "DELETE",
url: urlRequest,
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
}).done(function() {
console.log('removed');
// the follow function call an ajax with get request so as the list will be updated with the new one
ajaxRequestToGetNewData();
}).fail(function() {
console.log('fail');
})


The delete request above has a lot of information to delete, some text and some images and I am worried if
ajaxRequestToGetNewData()
will be called before the server finishes all of this stuff.

My question: Is the
.done()
method safe for the above situation? If not, is there any other jQuery method that will be executed only if the request finishes all of its stuff?

Answer

Yes, it's entirely safe.

The done() handler will not be called until the promise returned by the $.ajax method has been resolved, which will only happen after the server has finished processing and returns a 200 OK response. If any other response is received then the fail() handler will execute instead.

In both cases the server processing will have stopped before a response is received by the client.

Comments