Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

120
Vistas
Fetching Data with Page Markers in a Loop

I'm trying to fetch data from an API that only returns 100 items at a time and a marker to the next page. The API also has a field that contains the total number of items.

I am having trouble grasping even where to start designing this.

I know the remaining devices isn't updating after each call to get item marker.. what can I do?

  if(remainingItems>0){
       
        getItemMarker(tkn, jsonData["PagedItemList"]["NextMarker"]["_text"]).then( result => {
          console.log(result);
          jsonData = convertXMLtoJSON(result);
          htmlResponse = htmlResponse + "<br><b>Part 2</b>"
          htmlResponse = htmlResponse + htmlGetter(jsonData); //need to convert result before sending
          return htmlResponse;
          //I want to get part 3, 4..etc until I have no items left
        }); //how do I ".then" over and over until I have no more items?
      }
      else{
        return htmlResponse;
      }
      
//I tried this 

 // but results in infinite loop
      for(var i = 0; remainingItems>0; i++){
      
        getItemMarker(tkn, jsonData["PagedItemList"]["NextMarker"]["_text"]).then( result => {
          console.log(result);
          jsonData = convertXMLtoJSON(result);
          htmlResponse = htmlResponse + "<br><b>Part " + i + "</b>";
          htmlResponse = htmlResponse + htmlGetter(jsonData); //need to convert result before sending
          remainingItems = remainingItems - jsonData["PagedDeviceList"]["Items"]["Item"].length;
          console.log(remainingDevices);
        });
      }
        return htmlResponse;

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

First of all. It contains only 100 items for a reason. You should probably split the result into "pages", like in a typical webshop.

Anyways, imagine if there is a million items on a slow internet. You don't want to download all of that, and then show the result. Instead

  1. download part of it
  2. update the HTML,
  3. check if the last fetched answer had 100 items,
  4. if so, download the next part by using a recursive loop.

In other words: update the HTML as soon as each "segment" is downloaded.

It's hard to show anything specific so see this as abstract code:

var part = 0;

function updateHTML(htmlResponse) { // 2
  // code to update HTML, with either .innerHTML or .appendChild();
}

function downloadItems() { // 1
  fetch(a_promise)
  .then((result) => {
     let jsonData = convertXMLtoJSON(result);
     let htmlResponse = `<br><b>Part ${++i}</b>`;
     let convertedToHTML = htmlGetter(jsonData)
     updateHTML(htmlResponse + convertedToHTML)    // 2

     if (jsonData.length == 100) {  // 3
       downloadItems();             // 4
     }
  });
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda