I have an operation that I need to perform synchronously, but some of the information I need can only be obtained via Ajax.
I tried to make everything synchronous with a wait for the Ajax return. The problem is that while any function is running, Ajax events that handle the successful return are not executed.
Here's an example of what I'm doing:
function foo () {
var a = false;
$.ajax({
url: "foo" // substitua por qualquer URL real
}).done(function () {
a = true;
});
while(!a) { } // Isto é apenas para se ter uma espera.
alert(a); // Isso nunca vai executar
}
foo();
And this has generated me a race condition
: The function associated with the done
event will not run while while
is iterating, and while
will never end until the done
event does not run. / p>
Is there any way to reach my goal?