How do I do a jQuery blocking AJAX call without async = false?
http://bugs.jquery.com/ticket/11013#comment:40
The use of the Deferred/Promise functionality in synchronous ajax requests has been deprecated in 1.8. The $.ajax method with async: false is supported but you must use a callback parameter rather than a Promise method such as
.then
or.done
.
So, if you are using the success/complete/error handlers, you can still use async:false
. More info at the jquery ticket above.
As of jQuery 1.8, the use of async: false with jqXHR ($.Deferred) isdeprecated; you must use the complete/success/error callbacks.
http://api.jquery.com/jQuery.ajax/ (async section)
I found this a bit annoying... developers should be given the choice to make a blocking call with async: false
if its something the platform allows - why restrict it? I'd just set a timeout to minimize the impact of a hang.
Nonetheless, I'm using a queue now in 1.8, which is non-blocking, and works quite nicely. Sebastien Roch created a easy to use utility that allows you to queue up functions and run/pause them.https://github.com/mjward/Jquery-Async-queue
queue = new $.AsyncQueue(); queue.add(function (queue) { ajaxCall1(queue); }); queue.add(function (queue) { ajaxCall2(queue); }); queue.add(function() { ajaxCall3() }); queue.run();
In the first 2 functions I pass the queue
object into the calls, here's what the calls would look like:
function ajaxCall1(queue) { queue.pause(); $.ajax({ // your parameters .... complete: function() { // your completing steps queue.run(); } });}// similar for ajaxCall2
Notice the queue.pause();
at the beginning of each function, and queue.run()
to continue queue execution at the end of your complete
statement.
I assume you will be doing full form validation on submit anyway, so perhaps it's no problem if the as-you-type email validation is interrupted, and precedence given to the form submission.
I think what I'd do is abort the 'validateEmailRegistered' AJAX request when the user submits the form. Then, perform full server-side form validation as usual - including email validation.
My understanding of as-you-type validation is that it's a nicety, not a substitute for validating the form when it is submitted. So it doesn't make sense to me for it to block the form submission.