3

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 requests in order. This should give you more power over data contrasting the use of the DOM as a medium to act on the data. (Is there another way?, it has been a while since I used javaScript)

function asyncModule(func)
{
    "use strict";
    var t, args;
    t = func.timeout === undefined ? 1 : func.timeout;
    args = Array.prototype.slice.call(arguments, 1);
    setTimeout(function () {
        func.apply(null, args);
    }, t);
}

Now something must be wrong with my reasoning because here is what the specs says:

Synchronous XMLHttpRequest outside of workers is in the process of being removed from the web platform as it has detrimental effects to the end user's experience. (This is a long process that takes many years.) Developers must not pass false for the async argument when the JavaScript global environment is a document environment. User agents are strongly encouraged to warn about such usage in developer tools and may experiment with throwing an InvalidAccessError exception when it occurs. @ https://xhr.spec.whatwg.org/

I would think you would want to avoid async in requests at all costs and instead wrapp sync requests within async function.

Here is the main question along with the follow up.

  • Is there something wrong with the example I gave?

If not then:

  • How is forcing requests to be async the right solution?

It goes without saying that you have freedom to debunk any of my "claims" if they are simply wrong or half truths. I am confused over this, I give you that.

Keep in mind that I am testing javaScript in terminal, not in the browser. I used the webserver within GO programming language and everything seems to be working fine. It is not until I test the code within the browser that I get hint for this spec.

user1235831
  • 111
  • 1
  • 8

1 Answers1

0

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);
}
Community
  • 1
  • 1
user1235831
  • 111
  • 1
  • 8