“原子”操作受到异步 ajax 回调的干扰

发布于 2024-12-18 15:31:28 字数 1574 浏览 1 评论 0原文

我知道,在同一个句子中使用 JavaScript 和“原子”这两个词都有点奇怪,因为 JavaScript 被认为是异步的,因此不是很原子。

//编辑 这是我这边的一个错误!通过让警报消失(并在 Chrome 中隐藏更多警报),它会快速中断并让其他代码飞翔。 JavaScript 是单线程的。

快速 ->实际问题; 在什么情况下我们可以避免异步回调中断,以及如何防止某些代码块发生异步回调中断?

长->我的场景; 我的整个应用程序是非常递归的,会触发许多 ajax 请求,在返回时,会触发更多的递归函数,这些函数可能会触发更多的 ajax 请求。 在我的代码中,我对数组进行了一些非常关键的操作,这些操作必须在下一个操作发生之前完成(尽管是简单的推送/拼接逻辑)。

我遇到了一个问题,我获取了数组中键的索引并将其保存在变量中。然后我将它与 -1 进行比较,如果这是真的,我就拼接(不仅仅是取消设置)数组中的元素。 现在,在获取索引和拼接之间,异步回调返回结果并开始递归,然后通过添加/删除更多项目来更改数组(并弄乱我之前获得的索引值)。

这是旧代码;

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

这是“工作”,但不是优化的代码,

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

我只是简单地再次搜索索引并直接将其拼接起来。

我的一般问题是,在什么情况下我们可以避免异步回调中断,以及如何防止某些代码块发生异步回调中断?

StackOverflowers 的朋友们,我的具体问题是,理论上,ajax 回调是否可以在返回索引的 indexOf 函数和切割数组的 splice 函数之间调用。

感谢您的帮助

p.SI 我知道,我只需取消设置数组字段即可解决我的索引问题。但这不是我想要的,因为我正在序列化该信息并且不想要百分之一的空条目。找到解决此类情况的通用方法是我的目标:)

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.

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

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

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

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

This was the old 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
    ...
}

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.

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

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.

Thank you for your help

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 :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一场信仰旅途 2024-12-25 15:31:28

JavaScript 本质上是单线程的。这意味着如果 AJAX 响应到达或应触发超时/间隔但其他代码正在运行,则响应回调/超时将等待

这是 node.js 选择 JavaScript 的主要原因之一。

另请参阅:

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.

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

See also:

秋风の叶未落 2024-12-25 15:31:28

JavaScript 完全是单线程的。

异步回调只能在没有其他代码使用消息队列(如 Windows 消息)运行时运行。
所有代码都在 UI 线程上运行,并且您的代码永远不会在中间中断(“无响应脚本”警告除外)

请注意,Javascript 确实支持使用 HTML5 Web Workers 的真正多线程。

Javascript is entirely single-threaded.

Async callbacks can only run when no other code is running using a message queue, like Windows messages.
All code runs on the UI thread and your code can never be interrupted in the middle (except by the Unresponsive Script warning)

Note that Javascript does support true multi-threading using HTML5 Web Workers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文