Axios spread() with unknown number of callback parameters

独自空忆成欢 提交于 2019-12-20 09:36:51

问题


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?


回答1:


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);
  …
});


来源:https://stackoverflow.com/questions/37149466/axios-spread-with-unknown-number-of-callback-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!