带有未知数量的回调参数的Axios Spread()

编程入门 行业动态 更新时间:2024-10-22 20:26:48
本文介绍了带有未知数量的回调参数的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包含给定的字符串url,而不是实际的响应.我该如何解决这个问题?

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()

本文发布于:2023-10-27 11:28:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1533224.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:回调   数量   参数   Spread   Axios

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!