6

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?

7
  • 3
    no need for a counter due to promises. Take a look at $q.all(). $http returns a promise and you can wrap an array of promises and call done when they all complete Commented Jan 11, 2015 at 5:16
  • it has never happened with me, AFAIR, but i am waiting for an answer! good question! Commented Jan 11, 2015 at 5:18
  • 1
    @mplungjan that would be very slow Commented Jan 11, 2015 at 5:21
  • @mplungjab Your approach is too slow; why should each call wait for the last before calling out. I would like to know if your solution is necessary or if JavaScript handles it for us Commented Jan 11, 2015 at 5:35
  • @VictorGrazi that is not a common approach at all unless the calls are dependent on passing data to each other Commented Jan 11, 2015 at 5:36

2 Answers 2

5

No, it is not possible (no collision will occur). Each AJAX call response can be considered a message on the JavaScript message queue. The JavaScript runtime processes each message on the queue to completion before processing the next. In other words, it calls the callback for the event and runs the full callback function (and all the functions it calls, and so on) to completion before moving onto the next message in the queue. Therefore, no two messages (e.g., AJAX responses) will be processed in parallel.

Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/EventLoop

Also, I've worked in JavaScript for many years and can guarantee this is how it works.

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

1 Comment

I believe you, but can you please provide some explanation or source for your assertion?
3

like you mentioned, since javascript is single threaded, I do not think such a scenraio would happen, the only way js becomes multi threaded is when you use webworkers, but even they do not share the same variables( they clone the attribute js objects you pass, i am assuming here), so there should be any case of read/write overlap

EDIT

on second thought, say there is function onCounterIncreament which would do a bunch of operation when counter increments, if you do not call it immediately after you increamenting counter, but use $watch or something timeout fn to check for change in counter, there is a good possiblity you might miss a change.

2 Comments

My understanding is there are exceptions to the single threaded thing. For example, if several ajax calls return at the same time, or things like mouse events and such, where concurrency is required to ensure liveness
@VictorGrazi, "JavaScript may be asynchronous, which is true. But basic JavaScript without WebWorkers is never multithreaded. See Már Örlygsson's answer " -- stackoverflow.com/questions/2734025/…

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.