Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

119
Views
Obtener datos con marcadores de página en un bucle

Estoy tratando de obtener datos de una API que solo devuelve 100 elementos a la vez y un marcador para la página siguiente. La API también tiene un campo que contiene el número total de elementos.

Tengo problemas para comprender incluso dónde comenzar a diseñar esto.

Sé que los dispositivos restantes no se actualizan después de cada llamada para obtener el marcador de elementos... ¿qué puedo hacer?

 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 answers
Answer question

0

Ante todo. Contiene solo 100 artículos por una razón. Probablemente debería dividir el resultado en "páginas", como en una tienda web típica.

De todos modos, imagina si hay un millón de elementos en un Internet lento. No desea descargar todo eso y luego mostrar el resultado. En cambio

  1. descargar parte de ella
  2. actualizar el HTML,
  3. verificar si la última respuesta buscada tenía 100 elementos,
  4. si es así, descargue la siguiente parte usando un bucle recursivo.

En otras palabras: actualice el HTML tan pronto como se descargue cada "segmento".

Es difícil mostrar algo específico, así que vea esto como un código abstracto:

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!