最大并发请求限制

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script>

let currentIndex = 0
function requestLimit(requestList, maxNum) {
  currentRequstList = requestList.splice(maxNum)
  currentIndex = maxNum - 1
  for(item of currentRequstList) {
    requestMethod(item, requestList, maxNum, currentIndex)
  }
}

function requestMethod(obj, requestList, maxNum, currentIndex) {
  axios[obj.type](url, obj.data)
    .then(function (response) {
      console.log(response)
      afterRequest(requestList, maxNum, currentIndex)
    })
    .catch(function (error) {
      console.log(error)
      afterRequest(requestList, maxNum, currentIndex)
    })
}

function afterRequest(requestList, maxNum, currentIndex) {
  if(currentIndex === requestList.length -1) return
  currentIndex++
  requestMethod(requestList[currentIndex], requestList, maxNum, currentIndex)
}

let requestList = [{
  type: 'get',
  url: 'http:',
  data: {name: 'zs', age: 80}
}, {
  type: 'post',
  url: 'http',
  data: {name: 'ls', age: 81}
}]

requestLimit(requestList, 12)

</script>
</body>
</html>

评论

暂无评论