Skip to content Skip to sidebar Skip to footer

Case For Having Xmlhttprequest Available In Sync

One could simply encapsulate number of synchronous requests as an asynchronous request. The 'func' parameter within the below code could for example contain multiple synchronous re

Solution 1:

This answer has been edited.

Yes I my reasoning was faulty!

There are two angles to think about. What does async actually mean in javascript? Can one async call stall another async call?

Async in javascript doesn't mean script will be running in a interleaved/alternating processes with more then one callstack. It can be more like a global timed defer/postpone command that will fully take over once it get its chance. This means async call can be blocking and the nonblocking "async:true" part is only a "trick" based on how xhttprequest is implemented.

This means encapsulating a synchrounous request within setTimeout could be waiting for a failed request that ends up blocking other unrelated async requests where as "async:true" feature would only execute based on its state value.

This means older browser support requires you to chain requests or to use DOM as a medium when you need to do multiple requests that depend on another..Ugh...

Lucky for us, Javascript has threads now. Now we can simply use threads to get clean encapsulation of multiple correlated requests in sync. (or any other background tasks)

In short: The browser shouldn't have any problems of running request in sync if it is within a worker. Browsers have yet to become OS, but they are closer.

P.S. This answer is more or less because of trial and error. I made some test cases around firefox and observed async request do halt other async requests. I am simply extrapolating from that observation. I will not accept my own answer in case I am still missing something.

EDIT (Again..) Actually, it might be possible to use xhttp.timeout along with xhttp.ontimeout. See Timeout XMLHttpRequest This means you could recover from bad requests if you abstract setTimeout and use it as a schedular.

// Simple example
function runSchedular(s)
{
    setTimeout(function() {
        if (s.ptr < callQue.length) {
            // Handles rescheduling if needed by pushing the que.
            s = s.callQue[s.ptr++](s);
        } else {
            s.ptr = 0;
            s.callQue = [];
            s.t = 200;
        }
        runSchedular(s);
    }, s.t);
}

Post a Comment for "Case For Having Xmlhttprequest Available In Sync"