3

Theory: I have around 100 promises which I make in start and then later resolve them using Promise.all().

Each of those 100 promises in turn make some async REST calls whose response may vary mainly (for example due to network connectivity).

Process of resolving all 100 promises take around 20 sec. During that time user should be given a live feedback of progress to keep him engaged.

In order to implement progress of these async operations I am thinking of using a progressCounter on client end whose value will be updated by each promise as soon as its resolved.

Thing is that if progressCounter = 1 and as all those operations are async, I fear to hit a race condition where, for example, current value of progressCounter retrieved by two distinct promises might be found as same i.e. 1 so they may try to increment progressCounter to same value i.e. 2. So final value won't be 3 because of the race condition.

Experiment: I tried to reproduce this theory but couldn't using following:

var progress = {};
progress.counter = 1;

var promise1 = new Promise(function(resolve, reject) {
   resolve();
});

var promise2 = new Promise(function(resolve, reject) {
   resolve();
});

promise1.then(function() {
    progress.counter += 1;
});

promise2.then(function() {
    progress.counter += 1;
});

setTimeout(function() {
    alert(progress.counter); // progress.counter: 3
}, 1000);` 

Question: Question is can such a race condition be hit described in theory above? If not, how is the theory flawed? If yes, what is a good way to track progress of resolution of the promises?

Uthman
  • 9,251
  • 18
  • 74
  • 104
  • The code you posted seems to work. Can you produce a situation like you describe in actual code so we can test this? – raduation Aug 27 '15 at 22:24
  • Also, I don't see any code that checks the value of `progress.counter` before incrementing it, so I don't see how this race condition could happen. – raduation Aug 27 '15 at 22:25
  • 5
    JavaScript is single-threaded so two pieces of code can't execute simultaneously. And even if it were threaded, `+=` is a single operation so nothing could change the variable's value in between incrementing it anyway. – JJJ Aug 27 '15 at 22:26
  • 1
    See the "Is javascript guaranteed to be single-threaded?" thread http://stackoverflow.com/questions/2734025/is-javascript-guaranteed-to-be-single-threaded – wesww Aug 27 '15 at 22:35
  • @raduation the code doesn't hit the race condition as I mentioned I "couldn't" reproduce the theory. – Uthman Aug 27 '15 at 22:38
  • 1
    @Juhana if it was multi threaded, there could exist race condition.. `+=` is a single statement in javascript, but at a lower level, it's a set of non atomic instructions.. – Joaquín O Aug 27 '15 at 22:41

1 Answers1

7

Question: Question is can such a race condition be hit described in theory above? If not, how is the theory flawed?

The answer is no, such a race condition can not occur in Javascript, because Javascript is single-threaded. (see: Concurrency Model and Event Loop on MDN)

This means that while one callback handler is working with the data (assuming that setting the counter is a synchronous operation, which += is), nothing can force it to "yield" its execution, the next handler can only run when the previous one has finished.

doldt
  • 4,466
  • 3
  • 21
  • 36