const letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
// letters container
const lettersContainer = document.querySelector(".letters");
// add each alphabet into lettersContainer
letters.forEach(letter => {
// create span for letter
let span = document.createElement("span");
let spanText = document.createTextNode(letter);
span.appendChild(spanText);
lettersContainer.appendChild(span);
})
console.log("before getData")
// fetch words and category
async function getData(src) {
try {
let data = await fetch(src);
data = await data.json();
console.log(data)
return data
} catch (error) {
console.log(error)
}
}
// words.json is json that contain simple object of words
let wordsAndCategory = getData("./words.json");
console.log("after getData");
//the rest of code depends on object wordsAndCategory
when i execute code, it prints after getData before printing data. i have another solution which is putting the rest of code in async function but i am wondering if there is a better solution for this issue.