简体   繁体   English

如何在 JavaScript 中实现原子操作(并发 model)?

[英]How to achieve atomic operations ( Concurrency model ) in JavaScript?

Say, I want to increment a counter every time I get an ajax response.说,我想在每次收到 ajax 响应时增加一个计数器。 I don't know about the concurrency model with JavaScript async events.我不知道 model 与 JavaScript 异步事件的并发性。 Any thoughts?有什么想法吗?

Within a browser, there is only ever one javascript thread running at a time.在浏览器中,一次只运行一个 javascript 线程。

Concurrency issues are possible in that while an ajax response is pending, javascript may run in response to an unrelated event.并发问题可能是因为当 ajax 响应未决时,javascript 可能会运行以响应不相关的事件。 But you are safe if you do not start an operation intended to be atomic before an ajax call and finish it in the response handler.但是,如果您没有在 ajax 调用之前启动旨在成为原子的操作并在响应处理程序中完成它,那么您是安全的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM