迭代 API 调用直到找到结尾

Iterate an API call for until the end is found

我有一个 API 它 returns 有限的数据结果 限制值不能大于1000

> https://api.source.com?start=0&limit=1000 //first call
> https://api.source.com?start=1001&limit=1000 //second call
> https://api.source.com?start=2001&limit=1000 //third call
> https://api.source.com?start=3001&limit=1000 //fourth call

{
  "success": true,
  "message": "Verticals Returned",
  "response": {
    "start": 0,
    "limit": 1000,
    "returned": 1000,
    "total": 3230,
    "data": [
      {
        "verticalID": 3,
        "verticalName": "Galaxies",
        "status": "Active",
        "groupID": 1,
        "createdOn": "2022-03-15 05:30:06",
        "groupName": "Solar",
        "totalOffers": 0
      }
    ]
  }
}

如何使用循环来迭代它? while 或任何其他循环也可以。

我试过

limit =1000
start = 0;
total = undefined;
for(i=start;;i+=returned)
{
    y = call("https://api.source.com?start="+start+"&limit="+limit)
    total = y.total
    start = y.start
    returned = y.returned
}
const apiLoop = async () =>{
   let counter = 0;
   const limit = 1000;
   while (true) {
       try{
          let y = await call("https://api.source.com?start="+(counter*limit)+"&limit="+limit);

          if(y==null)  
              break;

          counter++;
       }
       catch(e){
          console.log(e.message);
          break;
       }
   }
}

apiLoop();

根据y变量得到的值,当start查询参数超过总长度时,打破循环的条件会有所不同.这里我假设y应该是null。 请注意,call 函数应该是 async 并在此代码块中等待。因为迭代中的每个 API 调用都应该先得到结果,然后根据结果,函数决定是否继续 while 循环。