1. 程式人生 > 實用技巧 >js 同一介面,合併多次請求

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>