带有未知数量回调参数的 Axios spread() [英] Axios spread() with unknown number of callback parameters

查看:24
本文介绍了带有未知数量回调参数的 Axios spread()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 axios 处理未知数量的 AJAX 请求(1 个或更多),但我不确定如何处理响应.我想要一些类似的东西:

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

其中参数将包含 axios 发送的回调响应.问题是 arguments 包含给定的字符串 urls 而不是实际的响应.我该如何解决这个问题?

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?

推荐答案

您需要在某个地方提出实际请求.然后不使用 spread 而只使用 then 来接收结果数组:

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

这篇关于带有未知数量回调参数的 Axios spread()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆