你能帮我解决我的代码吗?我需要并发运行请求,但get请求一个接一个。下面的代码示例(使用VueJS方法/函数)
async manageData() {
// array of chart's properties
const charts = await axios.post('/report/chart_data', {id: this.id);
// concurrent requests with variable 'charts'
const result = await Promise.all(
charts.map((chart, key) => axios.post('/report/data_range', {id: key}))
);
}
发布于 2019-05-08 01:30:43
const charts = await axios.post('/report/chart_data', {id: this.id);
将发出单个请求,并等待它完成,然后再继续。这是必要的,因为您随后将使用charts
响应来形成后续请求。
charts.map((chart, key) => axios.post('/report/data_range', {id: key}))
将使charts.length
数量的请求并行。根据您的描述,这是可取的,因为它看起来请求是独立的。
您的屏幕截图支持这种分析(第一个请求正在等待响应,然后才会并行进行后续请求)。
如果知道索引响应的长度,就可以并行化第一个和后续的请求,因为您只使用key
( charts
)而不是实际的chart
来执行后面的请求。
如果你关心为什么后面的请求似乎需要稳定增加的响应时间,这可能只是每个请求的复杂性的巧合,或者,就像用户ctt@在评论中指出的那样,可能表明你的后端没有同时处理这些请求。但这似乎不是你所关心的(根据你的评论)。
https://stackoverflow.com/questions/56027589
复制相似问题