Estoy tratando de usar setTimeout en un bucle for para que mis solicitudes HTTP se envíen una vez por segundo para evitar la limitación de velocidad. Sin embargo, no parece estar funcionando. ¿Podría alguien por favor ayudar?
async function initiateSearchExperimental() { const json = await getCollections(); for (let i = 0; i < json.result.data.length; i++) { setTimeout(getData(json, i), 1000 * i) } } function getData(json, i) { fetch(`https://howrare.is${json.result.data[i].url}/?for_sale=on&sort_by=rank`).then(function(response) { // The API call was successful! return response.text(); }).then(function(html) { // Convert the HTML string into a document object var parser = new DOMParser(); var doc = parser.parseFromString(html, 'text/html'); var priceArray = getPriceArray(doc.querySelectorAll("div.featured_item")) console.log(json.result.data[i].url, curateArrayTwo(priceArray, json.result.data[i])) }).catch(function(err) { // There was an error console.warn('Something went wrong.', err); }); }No hay necesidad de esperar y tal
Le sugiero que use setTimeout, pero hágalo en el éxito de la segunda búsqueda
let data; let cnt = 0; const getData() { if (cnt >= data.length) return; // stop fetch(`https://howrare.is${data[cnt].url}/?for_sale=on&sort_by=rank`) .then(function(response) { // The API call was successful! return response.text(); }).then(function(html) { // Convert the HTML string into a document object var parser = new DOMParser(); var doc = parser.parseFromString(html, 'text/html'); var priceArray = getPriceArray(doc.querySelectorAll("div.featured_item")) console.log(data[cnt].url, curateArrayTwo(priceArray, data[cnt])) cnt++ setTimeout(getData, 1000) }).catch(function(err) { // There was an error console.warn('Something went wrong.', err); }); }; fetch(collectionurl) .then(response => response.json()) .then(json => { data = json.result.data; getData() });