js 同一接口,合并多次请求

<html>
<title>新标签页111</title>

<body>
  <script>
  //正则判断字符串开头abc和结尾xyz(/^(abc)(.+)?(xyz)$/).test('abcxyz')
    async function fun1() {
      var newlist = [];
      var index = 4;
      for (var i = 0; i < index; i++) {
        const params = {
          CellID:i,
          Offset: i,
          Count:i,
          RankID:i,
        };
        var res = await requst(params)
        console.log(res, 'res')
        newlist = [...newlist, ...res.list]
        console.log(newlist, 'newlist')
        if (i == index - 1) {
          console.log(newlist)
          
        }
      }
    }

    function requst(params) {
      return new Promise((reslove, reject) => {
        setTimeout(function () {
          console.log(params,'params')
          var random = Math.ceil(Math.random() * 10)
          console.log(random, 'random')
          var res = {
            list: [{ name: "王" + random }],
          }
          reslove(res)
        }, 1000)
      })

    }
    fun1()
  </script>
</body>

</html>
原文地址:https://www.cnblogs.com/fanqiuzhuji/p/14151250.html