Im trying to fetch some data from an api problem is the render cards function begins to execute before the data have been saved for some reason, and when i try to render i get undefined since the cards are undefined.
when it did work it also returned the data stringified and not parsed for some reason
onInit happens on body load.
<body onload="onInit()">
function onInit() {
FetchCards();
renderCards();
}
function FetchCards(){
var cards =loadFromStorage(KEY)
if(!cards){
fetch('http://www.filltext.com/?rows=10&fname=%7bfirstName%7d&lname=%7blastName%7d&tel=%7bphone|format%7d&address=%7bstreetAddress%7d&city=%7bcity%7d&state=%7busState|abbr%7d&zip=%7bzip%7d&pretty=true')
.then(res => {
if(res.ok) res.json()
.then(data=>{
console.log(data);
saveToStorage(KEY,JSON.stringify(data))
})
})
}
}
function renderCards() {
var cards = getCardsForDisplay()....
function getCardsForDisplay(){
var cards =loadFromStorage(KEY)
return cards
}
function saveToStorage(key, val) {
localStorage.setItem(key, JSON.stringify(val))
}
function loadFromStorage(key) {
var val = localStorage.getItem(key)
if(!val || val==='undefined') return undefined;
return JSON.parse(val)
}