Let's say I need to make two async calls to different API endpoints. Query parameters of second call depend on what I receive from the first one
Quick overview:
First endpoint provides some ids in specific order (sorted by rating, for example)
Second endpoint provides some "meta information" about those ids
Endpoint examples:
GET/endpoint/one
response format:
[
{
id: 1,
rating: 0.67
},
{
id: 2,
rating: 0.51
},
{
id: 3,
rating: 0.45
},
...
]
GET/endpoint/two?id=1,2
// I receive those ids from call to the first endpoint
response format:
[
{
id: 1,
gender: "male",
age: 20,
profession: "Programmer"
},
{
id: 2,
gender: "transgender",
age: 27,
profession: "ML Engineer"
}
]
Afterwards I need to loop through the all the ids I have and display meta info in my template, but order should be the same as it was in response of the endpoint/one
I decided to crate a key: val storage for those needs, i.e:
const storage = {
1: {
gender: "male",
age: 20,
profession: "programmer"
}
}
also I have a variable with persons from first endpoint, i.e:
const persons = [
{
id: 1,
rating: 0.67
},
...
]
so in my template I could do smth like
<p v-for="p in persons">{{ storage[p.id].gender }}</p>
The question is: how I could chain my requests to have all the data available after I call some function, which initiates my requests?
*Keep in mind that I could query the second endpoint only with two ids, so I will need to loop through the list of ids I have and make a separate call for each one. In a real app I have ~ 100 ids from the first endpoint, so I need to make ~ 50 calls to the second one
Promise.allto make sure you have all the data before proceeding. Smarter would be to declare mutations for adding ids, and render them as they come.