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 pleted.
My concern is that since JavaScript does not have anything parable 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 e 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?
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 pleted.
My concern is that since JavaScript does not have anything parable 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 e 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?
Share Improve this question edited Jan 11, 2015 at 5:30 Victor Grazi asked Jan 11, 2015 at 5:14 Victor GraziVictor Grazi 16.6k15 gold badges67 silver badges98 bronze badges 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 plete – charlietfl Commented Jan 11, 2015 at 5:16 - it has never happened with me, AFAIR, but i am waiting for an answer! good question! – shaheer Commented Jan 11, 2015 at 5:18
- 1 @mplungjan that would be very slow – shaheer 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 – Victor Grazi Commented Jan 11, 2015 at 5:35
- @VictorGrazi that is not a mon approach at all unless the calls are dependent on passing data to each other – charlietfl Commented Jan 11, 2015 at 5:36
2 Answers
Reset to default 5No, 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 pletion 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 pletion 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/en-US/docs/Web/JavaScript/Guide/EventLoop
Also, I've worked in JavaScript for many years and can guarantee this is how it works.
like you mentioned, since javascript is single threaded, I do not think such a scenraio would happen, the only way js bees 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.