I want to fetch API using axios that return array list of objectId. After I get the list objectId I want to fetch the detail of the object using promise
I think of something like this
var objectDetail = [];
axios.get('apiendpoint/')
.then((response) => {
var listOfObjectId = response.data;
var chain = Promise.resolve()
for (var objectId of listOfObjectId) {
chain = chain.then(axios.get(`apiendpoint/${objectId}`)
.then((response) => {
objectDetail.push(response.data);
})
);
}
return chain;
}).then((chain) => {
console.log(chain);
return chain;
})
the code above returns undefined, the promise chain object is not passed to then
method call. Is my approach wrong or am I missing something? thank you
Here are some stack I have read, might be related: