"原子"操作desturbed异步AJAX回调 [英] "atomic" operation desturbed by asynchronous ajax callbacks

查看:132
本文介绍了"原子"操作desturbed异步AJAX回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,使用同一句中的JavaScript和'atomic',什么是有点儿奇怪,因为JavaScript是珍贵的是异步的,并为此不太原子。

I know, using the words JavaScript and 'atomic'-anything in the same sentence is kinda strange, since JavaScript is prised to be asynchronous and therefor not very atomic.

//编辑 这是站在我这边的错误!通过让警报的持续关(和隐藏进一步警报镀铬),它很快中断,并让其他code飞。 JavaScript是单线程的。

//EDIT This was a mistake on my side! by having alert's going off (and hiding further alerts in chrome) it quickly interrupted and let other code fly. JavaScript is single-threaded.

快 - >实际问题; 在这种情况下,我们是从节省异步回调中断和我们怎么能prevent他们对某些code块?

Quick -> Actual Question; in which situation are we save from async callback interrupts and how could we prevent them for certain code blocks?

龙 - >我的方案; 我的整个应用程序是非常递归并触发许多Ajax请求,即在返回时,会触发更多的递归函数,可能会引发更多的Ajax请求。 在我的code,我有一个数组一些非常关键的操作,即必须完成之前的一个操作都有可能发生(虽然简单推/拼接逻辑)。

Long -> My Scenario; My whole application is very recursive and triggers many ajax requests, that on returns, triggers many more recursive functions that may trigger more ajax requests. In my Code, I have some very crucial operations on an array, that have to complete before the next operation can happen (simple push/splice logic though).

我有一个问题,在这里我得到了一个重要的指标一个阵列内并保存在一个变量。然后,我将它比作-1,如果这是真的,我拼接(不只是未设置)从数组中的元素。 现在,在获取指数和拼接之间,异步回调返回与结果,并开始递归的东西,然后通过添加/删除其他项目(和搞乱了我之前的索引值),改变了阵列。

I had a problem, where I got the index of a key within an array and saved it in a variable. I then compared it to -1 and if it was true, I spliced (not just unset) the element from the array. Now, in between getting the index and splicing, the asynchronous callback returned with results and started recursive stuff, which then altered the array by adding/removing further items (and messing up the index value I got before).

这是老code;

if ( this.dataset && (index=this.dataset.children.indexOf(child.key) )!==-1 ){
    console.log("removed from dataset!");
    //<=== AJAX Call Returns and adds/removes items from the array
    this.dataset.children.splice(index, 1); //goes bad, because index not good anymore
    ...
}

这就是工作,而不是优化code

and this is the 'working', but not optimized code

if ( this.dataset && (index=this.dataset.children.indexOf(child.key) )!==-1 ){
    console.log("removed from dataset!");  
    //<=== AJAX Call Returns and adds/removes items from the array
    //Problem solved, since I'm getting the index again
    this.dataset.children.splice(this.dataset.children.indexOf(child.key), 1);
    ...
}

我只是单纯的搜索指数再次直接拼接它扔掉。

I just simply search for the index again and directly splice it away.

我一般的问题是,在这种情况下,我们是从节省异步回调中断和我们怎么能prevent他们对某些code块?

my general question is, in which situation are we save from async callback interrupts and how could we prevent them for certain code blocks?

我的具体问题,老乡StackOverflowers,如果是在理论上,Ajax回调可以被称为之间的的indexOf函数返回的索引和拼接功能切割阵列了。

My specific question, fellow StackOverflowers, is if in theory, the ajax callback could be called in between the indexOf function returning the index and the splice function cutting the array up.

感谢您的帮助

PS我知道,我只可以取消设置数组字段和我的索引的问题将得到解决。但是,这不是我想要的,因为我连载的信息,不想空项100分之。找到一个一般的方式如何应对这种情况是我的目标:)

p.S I am aware, that I just could unset the array field and my index problem would be solved. But that's not what I want, since I'm serialising that information and don't want 100ths of empty entries. Finding a general way how to tackle such situations is my goal :)

推荐答案

JavaScript是固有的单线程。这意味着,如果Ajax响应到达或超时/间隔应被触发,但其他code运行时,响应回调/超时将

JavaScript is inherently single-threaded. This means that if AJAX response arrives or timeout/interval should be triggered but other code is running, the response callback/timeout will wait.

这是主要的原因,JavaScript是选择一个 Node.js的

This was one of the primary reasons why JavaScript was chosen for node.js.

另请参阅:

这篇关于&QUOT;原子&QUOT;操作desturbed异步AJAX回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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