T
TCGPlaza
Guest
TCGPlaza Asks: setTimeout in asynch fetch not working (javascript)
I have a list of 16k or so objects I'm trying to fetch, no issues whatsoever fetching ONLY the id. But when I try to fetch the data for every id, my chrome can't handle it. I've looked through a dozen threads on here and whenever I try to add a new promise it either tells me it's invalid or that it only goes at the top or bottom. When I add a regular timeout (1 second) it just waits for example 1 second before fetching ALL 16k objects...
I'd appreciate if someone could help me solve this but also explain why it waits 1 second before going through everything at once INSTEAD of taking 1 second PER record.
I should mention that my setTimeout is placed where it's placed for the simple reason that it wouldn't "run" anywhere else.
I have a list of 16k or so objects I'm trying to fetch, no issues whatsoever fetching ONLY the id. But when I try to fetch the data for every id, my chrome can't handle it. I've looked through a dozen threads on here and whenever I try to add a new promise it either tells me it's invalid or that it only goes at the top or bottom. When I add a regular timeout (1 second) it just waits for example 1 second before fetching ALL 16k objects...
I'd appreciate if someone could help me solve this but also explain why it waits 1 second before going through everything at once INSTEAD of taking 1 second PER record.
Code:
async function fetchMODEL(endpoint) {
var array;
const whatever = await fetch("https://link.com/" + endpoint)
.then((response) => response.json())
.then((data) => (array = data));
return whatever;
}
function detectIDs(){
fetchMODEL("cars")
.then((response) => {
var data = response;
var allIds = data.map(item =>
{return item.id;})
//console.log(JSON.stringify(allIds))
//return allIds;
var illus = [];
for(i=0; i < allIds.length ;i++){
//setTimeout(
fetchMODEL("cars/" + allIds[i])
.then((response) => {
//console.log(typeof response.illustrator)
var artistData = {};
if(typeof response.illustrator == "undefined"){
artistData.edit="EDIT";
artistData.name = response.name;
artistData.id = response.id;
illus.push(artistData);
console.log(artistData)
}else{
artistData.id = response.id;
artistData.illustrator = response.illustrator;
artistData.name = response.name;
illus.push(artistData);
console.log(artistData);
}
})//,100)
}
})
}
var orgSetIDs = detectIDs();
I should mention that my setTimeout is placed where it's placed for the simple reason that it wouldn't "run" anywhere else.
SolveForum.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your thoughts here to help others.