From jQuery documentation:
As of jQuery 1.8, the use of async: false with jqXHR ($.Deferred) is deprecated; you must use the success/error/complete callback options instead of the corresponding methods of the jqXHR object such as jqXHR.done() or the deprecated jqXHR.success().
So what I understood from it is:
//deprecated usage
$.ajax({
url: "test.html",
async:false,
context: document.body
}).done(function() {
//code to be executed after response received .
});
//valid usage
$.ajax({
url: "file.php",
type: "POST",
async: false,
success: function(data) {
//means http request status successful
//code to be executed after response received .
// my code
}
});
From jQuery docs:
By default, all requests are sent asynchronously (i.e. this is set to true by default). If you need synchronous requests, set this option to false. Note that synchronous requests may temporarily lock the browser, disabling any actions while the request is active.
My question is:
As I understand, in both cases code will be executed only after response is received. Both will lock the the browser as async:false. Then why one is valid and one is not? I am trying to understand what make the valid usage better or am I understanding this all wrong?
I know synchronous request are not good (as they block UI) and are frowned upon. But sometimes client requirement forces your hand. For a registration form, I had implemented, I was asked to use google reCAPTCHA. Now the client wanted to inform the user if they had typed the wrong captcha before submitting. I implemented this by using synchronous ajax inside the onsubmit JavaScript function. How could this be done asynchronously? Can this be implemented differently without some kind of blocking? I am looking for alternate ideas of how this could be accomplished not code for this question.
While googling, I did find other SO question (links) on this, but could not find an answer which specified the difference between the valid and deprecated usage.