Let's say I wanna get some data from 1000 servers. Each server returns the same json structure. Dummy Example:
{
logins: 5000,
clicks: 1000
}
Let's say that I need to sum all logins from each server response.
Instead of query all the json and then perform the sum, I would like to do it in every callbacks (= 1000 times). Basic Example:
var result = 0;
$http.get(url).then(function(response) {
result += response.data.logins;
});
An example to explain why the lock is necessary:
If my first server returns 1000, the second 2000 and the third 3000;
Let's say the second hasn't finished yet when the third callback is called my the promise, for whatever reason.
If there is no lock on result, it will probably equal 4000 at the end of the third callback, which is wrong (the correct value is 6000);
So what do you think guys ? Is the result locked automatically or not ? If not, is it easy to create the lock pattern in js ?
Thanks for your answers !