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
 |  Show 1 more ment

1 Answer 1

Reset to default 6

Javascript 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