admin管理员组文章数量:1323715
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 plete 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 pared 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 :)
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 plete 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 pared 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 :)
Share Improve this question edited Sep 22, 2014 at 20:49 japrescott asked Nov 30, 2011 at 22:01 japrescottjaprescott 5,0233 gold badges28 silver badges38 bronze badges2 Answers
Reset to default 7JavaScript 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:
- Are there any atomic javascript operations to deal with Ajax's asynchronous nature?
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.
本文标签: javascriptquotatomicquot operation desturbed by asynchronous ajax callbacksStack Overflow
版权声明:本文标题:javascript - "atomic" operation desturbed by asynchronous ajax callbacks - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742124457a2421873.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论