After making an API call to retrieve a set of data such as a list of users, I noticed that I am implementing a for loop and within it, I am making another API call to obtain each user's profile details based on their ID. I understand that this approach is not ideal and I am seeking advice on how I can refactor it more efficiently.
api.get(...).then(response => {
this.users = response;
for(let i=0; i<this.users.length; i++){
api.get(...this.users[i].id).then(response => {
if(response.name == this.users[i].name)
this.newList.push(response);
})
}
})
Subsequently, in my HTML code, I iterate over this.newList to display the necessary information.
I am interested in finding a way to eliminate the nested API call from within the for loop while achieving the same desired outcome. How should I go about approaching this?