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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM