Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
105
rated 0 times [  108] [ 3]  / answers: 1 / hits: 20277  / 8 Years ago, tue, may 10, 2016, 12:00:00

I need to process an unknown number of AJAX requests (1 or more) with axios, and I am not sure how to handle the response. I want something along the lines of:



let urlArray = [] // unknown # of urls (1 or more)

axios.all(urlArray)
.then(axios.spread(function () {
let temp = [];
for (let i = 0; i < arguments[i].length; i++)
temp.push(arguments[i].data);
}));


where arguments will contain the callback responses sent by axios. The problem is that arguments contains the given string urls instead of the actual responses. How can I resolve this problem?


More From » ajax

 Answers
165

You somewhere will need to make the actual requests. And then don't use spread but only then to receive the array of results:



let urlArray = [] // unknown # of urls (1 or more)

let promiseArray = urlArray.map(url => axios.get(url)); // or whatever
axios.all(promiseArray)
.then(function(results) {
let temp = results.map(r => r.data);

});

[#62228] Monday, May 9, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
anyssaarielles

Total Points: 415
Total Questions: 107
Total Answers: 92

Location: Greenland
Member since Fri, Jul 31, 2020
4 Years ago
;