I am trying to make a bunch of axios get calls with this code:
console.log("getting here")
const promises = Data?.stuff.map(async(id: any) => {
axios.get('http://127.0.0.1:8080/api/v1/stuff/' + id)
.then(response => {
console.log(response.data)
return response.data;
});
const tempLesson: any = await Promise.all(promises);
console.log("never getting here")
but it does not execute until the end. Whats the problem?
3
Answers
Your code are missing return statement and closing brackets
Your previous code should not compile. I don’t know how the first
console.log
can run.You can use axios asynchronously without extra promises
The Promise.all(promises) should be awaited outside of the .map() function. What Promise.all() does is it takes an array of promises (in your case, an array of Axios requests) and only resolves when all the promises in the array have resolved, or one has rejected.
so, if you want to do it:
That’s all.