4

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 becomes obsolete/bad since two operations can be modifying the variable at the same time.

Marc
  • 542
  • 8
  • 14
  • unpossible, javascript runs single threaded. could be cached. how are you counting? Because it should be with an [interceptor](http://docs.angularjs.org/api/ng.$http) – calebboyd Jan 21 '14 at 16:25
  • I'm doing $scope.count += 1 before each request, and $scope.count -= 1 after each response – Marc Jan 21 '14 at 16:29
  • You can print out start, success, error, and count to console. Then watch what happens. – allenhwkim Jan 21 '14 at 17:46
  • 1
    Why do you count http requests? Maybe (just guessing) counting is not best solution for your problem? – Igor S. Jan 21 '14 at 17:47
  • 1
    I'm not sure what your use-case is, but you might be interested in http://docs.angularjs.org/api/ng.$q – mmattax Jan 21 '14 at 17:53
  • @igor-s I just need a way to keep track of the requests I'm making, since I was making too many of them. – Marc Jan 22 '14 at 13:21

1 Answers1

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

calebboyd
  • 5,744
  • 2
  • 22
  • 32
  • Where can I read on how JS actually executes promises in the event loop? For instance, it is theoretically possible unless guaranteed otherwise, that in a middle of a function it is suspended and another is run, then the former is resumed Also, if JS promises are run in one by one, then it has to be wrong that Angular's $q.all executes its promises in parallel. But in this case, questions like http://stackoverflow.com/questions/34186545/how-can-i-limit-angular-q-promise-concurrency# don't make sense. Or does execution actually happens in parallel if run not in a browser but, say, node.js? – Maksim Gumerov Mar 13 '16 at 04:01
  • 1
    You can read about it [here](https://github.com/getify/You-Dont-Know-JS/blob/master/async%20&%20performance/ch1.md#event-loop) – calebboyd Mar 25 '16 at 22:01
  • 1
    Thanks! So if I got that right, no JS code is running concurrently with other JS code, **but** non-JS activity like XHR communications can actually be performed concurrently by the hosting environment. So, for instance, async XHR request can actually be run concurrently with JS code, then its callback is positively run non-concurrently with JS code. – Maksim Gumerov Mar 27 '16 at 05:43