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.
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.