0%

promise分步请求接口

背景

并发请求后台接口,处理时间过长,导致服务器超载挂掉,尤其是用户刷新页面操作,需要前端限制请求并发

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
let arr = [
{ url: 'xxx', data: {} },
{ url: 'xxx', data: {} },
{ url: 'xxx', data: {} },
{ url: 'xxx', data: {} },
{ url: 'xxx', data: {} },
{ url: 'xxx', data: {} },
{ url: 'xxx', data: {} }
]
async function SubmitMsg(datas, count) {
while (datas.length > 0) {
let urls = datas.splice(0, count)
let ret = await Promise.all(urls.map(getData))
console.log(ret);

}
}

function getData({ url, data }) {
return axios.post(url, data)
}

SubmitMsg(arr,2) //每次发送两个请求

欢迎关注我的其它发布渠道

来发评论吧~
Powered By Valine
v1.5.2