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.

6
  • unpossible, javascript runs single threaded. could be cached. how are you counting? Because it should be with an interceptor Commented Jan 21, 2014 at 16:25
  • I'm doing $scope.count += 1 before each request, and $scope.count -= 1 after each response Commented Jan 21, 2014 at 16:29
  • You can print out start, success, error, and count to console. Then watch what happens. 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? 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.org/api/ng.$q Commented Jan 21, 2014 at 17:53

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

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 stackoverflow.com/questions/34186545/… don't make sense. Or does execution actually happens in parallel if run not in a browser but, say, node.js?
You can read about it here
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.