I have a situation where I am making several (say four) ajax calls (using AngularJS http get, if that matters) and I want each call to callback and increment a counter, so I can know when all (four) of the threads have completed.
My concern is that since JavaScript does not have anything comparable to Java's "synchronized" or "volatile" keywords, that it would be possible for multiple concurrent threads to collide while incrementing a counter, thereby missing some increments.
In other words, two threads come at the same time, and both read the counter, getting the same value (for example 100). Then both threads increment that counter (to 101) and store the new value, and behold, we missed a count (101 instead of 102)!
I know that JavaScript is supposed to be single threaded, but there are exceptions.
Is this situation possible in the browser, and if so, is there any way to guard against it?
$q.all()
.$http
returns a promise and you can wrap an array of promises and call done when they all complete – Gambol