I'm new to promises and could use some help. I have a situation where promises are resolving randomly, how can I ensure that the last promise in the loop is resolved after the full loop executes?
For example, if this.selectedValues has 4 values, sometimes only three get updated and sometimes all four get updated. This inconsistency is due to promises.
let promise;
private values: Object[] = [];
_.each(this.selectedValues, (selectedValue) => {
promise = that.valueService.getValuesForName(selectedValue).then((pg) => {
values.push({
Id: pg.Id,
Name: pg.Name,
Description: pg.Description,
Policies: pg.Policies
});
});
});
if (promise !== undefined) {
promise.then(() => { // I want to call the promise only once for the last index
let data: Object = {
Id: that.id,
Name: name,
Description: description,
Values: values
};
that.updateEdit(data, name);
});