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