I have an array of objects that each need data from an api call.
The api endpoint is different for each object.
I am iterating the array of objects and creating a promise for each.
I am then using Promise.all passing an array of promises.
Once the promises have resolved, I need to update the original array of objects with their respective data. To prevent iterating the object array twice, I am assigning (for want of a better word) the result of then on the Promise before it is resolved.
It works, but are there any pitfalls with this approach?
updateData = async (objects) => {
const promises = [];
objects.forEach(object => {
if (object.data === undefined) {
const service = this.serviceFactory.getService(object.serviceKey);
promises.push(service[object.serviceFunc](object.id).then(data => object.data = data));
}
});
await Promise.all(promises);
};