admin管理员组文章数量:1327975
I have an angular app that does lots of asynchronous calls using $http.get, and I have a count variable in my scope ($scope.count) that keeps track of how many requests I made and how many are still pending. Obviously, I'm using it like so:
- Before I make a $http.get request I increment the count by 1
- When I get a response from the $http.get I decrement the count by 1
I'm making lots of requests, something around 2000 requests all at the same time, but the $scope.count value is not going back to 0 even after all requests are done, it's always greater than 0 (always off by 1 or 2). I'm handling both success and error events for my $http.get call, and I decrement the count whenever one of them happens.
So I was wondering if angular/javascript handle concurrency well? I'm thinking I'm running the increment operation so many times (potentially many at the same time) and the value of $scope.count bees obsolete/bad since two operations can be modifying the variable at the same time.
I have an angular app that does lots of asynchronous calls using $http.get, and I have a count variable in my scope ($scope.count) that keeps track of how many requests I made and how many are still pending. Obviously, I'm using it like so:
- Before I make a $http.get request I increment the count by 1
- When I get a response from the $http.get I decrement the count by 1
I'm making lots of requests, something around 2000 requests all at the same time, but the $scope.count value is not going back to 0 even after all requests are done, it's always greater than 0 (always off by 1 or 2). I'm handling both success and error events for my $http.get call, and I decrement the count whenever one of them happens.
So I was wondering if angular/javascript handle concurrency well? I'm thinking I'm running the increment operation so many times (potentially many at the same time) and the value of $scope.count bees obsolete/bad since two operations can be modifying the variable at the same time.
Share Improve this question asked Jan 21, 2014 at 16:20 MarcMarc 5409 silver badges14 bronze badges 6- unpossible, javascript runs single threaded. could be cached. how are you counting? Because it should be with an interceptor – calebboyd Commented Jan 21, 2014 at 16:25
- I'm doing $scope.count += 1 before each request, and $scope.count -= 1 after each response – Marc Commented Jan 21, 2014 at 16:29
- You can print out start, success, error, and count to console. Then watch what happens. – allenhwkim Commented Jan 21, 2014 at 17:46
- 1 Why do you count http requests? Maybe (just guessing) counting is not best solution for your problem? – Igor S. Commented Jan 21, 2014 at 17:47
- 1 I'm not sure what your use-case is, but you might be interested in docs.angularjs/api/ng.$q – mmattax Commented Jan 21, 2014 at 17:53
1 Answer
Reset to default 6Javascript runs single threaded (an event loop) so the concurrency problem is not possible.
What you should try to do is use an interceptor. The documentation has a great example.
You can put your count on $rootScope
本文标签: javascriptConcurrency in AngularjsStack Overflow
版权声明:本文标题:javascript - Concurrency in Angularjs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742251941a2440925.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论