I need to process each element of an array, making a HTTP call for each element and need to track status of the HTTP call for each call and update UI when everything completes. Currently I have the following code
for (var singleData of this.someData) {
this._Service.call(singleData.someData).subscribe(
data=> {
this.successGroups.push(singleData);
this.checkState();
},
error=> {
this.failureGroups.push(singleData);
this.checkState();
}
)
}
this._Service is a simple service which makes a Angular2 HTTP call and returns the observable. The objectives are as follows:
- Make calls for each item of list
- If success mark as success, failures as failure
- Update UI once all calls finish(success/failure is immaterial.)
Problem with above code is that since "singleData" gets updated, incorrect values get pushed. For example by the time subscriber is executed for 1st object of list "singleData" points to say 10th item of list and the 10th item gets instead instead of first.