Theory:
I have about 100 promises that I do at the beginning, and then resolve them using Promise.all().
Each of these 100 promises, in turn, creates several asynchronous REST calls, whose response can vary mainly (for example, due to network connectivity).
The process of resolving all 100 promises takes about 20 seconds. During this time, the user should be provided with direct feedback on the progress in order for him to help him.
To implement the progress of these asynchronous operations, I mean using the client-side progressCounter, the value of which will be updated for each promise immediately after its resolution.
The fact is that if progressCounter = 1 and, since all these operations are asynchronous, I'm afraid to get into a race state, where, for example, the current progressCounter value received by two different promises can be found as the same 1, so they may try to increase progressCounter to the same value, that is 2. Thus, the final value will not be 3 due to the race condition.
Experiment:
I tried to reproduce this theory, but could not use the 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);
}, 1000);`
Question
Question: Can such a race condition fall into the theory described above? If not, how is the theory wrong? If so, what is a good way to track the progress of resolving promises?