Race condition with jQuery.ajax({async:false})
I have just tested this code (abstraction):
$.ajax({
...
async: false,
success: function(data) {
// do some sluggish post-processing
alert(2);
}
});
alert(1);
And on my multi-processor, multi-threaded box I get the alerts in order 1, 2. I frankly expected 2, 1.
It therefore seems to me that a synchronous $.ajax call returns as soon as the data has been received, and not (as I think is very reasonable to expect) when the success() or error() function has returned. This throws up problems with any expensive data processing (eg JSON parsing) but even with the most basic code like
$.ajax({
success: function(data) {
globData= data;
}
alert(globData[0]);
the access to globData[0] becomes subject to a race condition.
This renders synchronous Ajax calls practically useless. Has anyone found a work-around?
Thanks.
PS: At a very minimum, the async option in $.ajax requires a much more nuanced manual entry...