I have an axios function, which I want to call several times, then I want to firstly sort received data and then store it. But now all my promises results are undefined.
const arr = {...checkbox};
const keys = Object.keys(arr).filter((k)=>{
return arr[k] == true;
});
let promises = [];
for (let i=0;i<keys.length;i++) {
promises.push(
axios.get(`my/api/${keys[i]}`).then(
function(response){
response.data.sort()
}
).catch((err) => error.value = err)
)
}
Promise.all(promises).then(() => console.log(promises));
2
Answers
The issue with your code is that you are not returning the sorted data from the Axios promise. You are sorting the data within the promise but not returning it.
In this code above the
axios
promise returns the sorted data using the return statement, and thePromise.all()
method waits for all promises to resolve and returns an array of sorted dataYou are not returning the data from your
then
clause. Try this:and then show it like this