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?