案例具有同步xmlhtt prequest可用 [英] Case for having xmlhttprequest available in sync

查看:237
本文介绍了案例具有同步xmlhtt prequest可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以简单地封装同步请求的数量作为异步请求。

One could simply encapsulate number of synchronous requests as an asynchronous request.

下面code中的FUNC参数例如可以包含多个订单同步请求。这应该给你更多的权力过的数据对比使用DOM作为媒介作用于数据。 (有另一种方式?它已经一段时间,因为我使用的JavaScript)

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:

工人外同步XMLHtt prequest是在从网络平台被除去,因为它有不利影响到最终用户的体验的过程。 (这是一个长期的过程,需要许多年。)当全球的JavaScript环境是一个文档环境,开发人员必须不及格的异步false参数。我们强烈建议用户代理警告开发人员工具的使用等,可与当它发生时抛出一个异常InvalidAccessError试验。 @ https://xhr.spec.whatwg.org/

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/

我想你会希望避免不惜一切代价异步请求,而是wrapp异步函数内同步请求。

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?

如果没有,那么:


  • 如何迫使请求是异步正确的解决方案?

  • 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.

请记住,我在终端测试JavaScript中,没有在浏览器中。我曾经在GO编程语言中的Web服务器,一切似乎是工作的罚款。它不是直到我考,我得到暗示了该规范的浏览器中的code。

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.

推荐答案

这个答案已被编辑。

是我我的推理是错误的!

有两个角度来思考的问题。
什么是真正的异步javascript中意味着什么呢?
可以异步调用另一个摊主异步调用?

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

异步JavaScript中并不意味着脚本将在交错/交替过程有一个以上的调用堆栈上运行。它可以是更像是一个全球性的定时延迟/推迟命令,一旦得到它的机会,将全面接管。这意味着,异步调用可以阻塞和非阻塞异步:真正的部分只有基于xhtt prequest是如何实现一个绝招

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.

这意味着封装内的setTimeout一个synchrounous请求可能会失败请求最终阻止其他不相关的异步请求,其中的异步:真正的功能不仅会执行基于其状态值。

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.

这意味着旧的浏览器支持要求您链请求或当你需要做的依赖another..Ugh ...

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...

我们是幸运的,使用Javascript目前拥有的线程。现在,我们可以简单地使用线程来获得同步多个相关请求的干净封装。 (或者任何其他的后台任务)

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。这个答案是多还是少,因为试错。我做了周围的一些Firefox的测试案例和观察到的异步请求暂停做其他异步请求。我只是从观察推断。我不会接受我自己的答案的情况下,我还是失去了一些东西。

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.

编辑(再次..)
实际上,有可能与xhttp.ontimeout沿使用xhttp.timeout。请参见超时XMLHtt prequest
这意味着你可以从错误的请求恢复,如果你抽象的setTimeout和使用它作为一个程序器。

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);
}

这篇关于案例具有同步xmlhtt prequest可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆