this may be a stupid question, but....is there a way in Javascript to synchronously wait for a specific request to finish without locking the browser thread?
The goal is to call server-side using ajax and executing piece of code after the call is finished AND avoiding callback (hell). Some simple piece of code like this.
// some js code
var result = doServerCall(); // w/out (b)locking the browser thread -> browser must remain responsive
// some js code to process the result
Please note setTimeout and setInterval is not acceptable solution, what is needed is straightforward execution as above. Eventually a callback after which the execution would continue at the point where the call to server has been done is also ok (see below).
I was using the following in a Firefox Add-on (which is not exactly the thing i want, but is still an acceptable solution).
globalDone = false;
// some js code
doServerCall(); // asynchrnonous call here, the callback is below
var thread = Cc["@mozilla.org/thread-manager;1"].getService(Ci.nsIThreadManager).currentThread;
while ( globalDone === false ) {
thread.processNextEvent(true);
}
// some js code to process the result
the callback
function processResponse ( xhrResponse ) {
globalResult = xhrResponse;
globalDone = true;
}
Going through the Internet, StackOverflow and forums everybody seems to want this, yet no browser looks to implement it