Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

156
Visualizações
Iterate an API call for until the end is found

I have an API where it returns limited results of data the limit value cannot be greater than 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
      }
    ]
  }
}

How do it iterate this using a loop? while or any other loop is also fine.

I have tried with

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
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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();

Depending on what value y variable gets, when start query parameter is more than total length, the condition of breaking the loop will be different. Here I assume that y should be null. Note that call function should be async and awaited in this code block. Because each API call in the iteration should get the result first and then based on the result, the function decides, whether to continue the while loop or not.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda