I am very confused as why my async function does not wait before running the code inside .then
For example, cards should be an array with 102 elements. However, console.log(cards.length) shows 0. Also, the last .then gets excused before any element is put into cards.
React.useEffect(() => {
// if (!cards.length) {
//https://api.pokemontcg.io/v2/cards?q=set.id:base1
async function fetchCards() {
const response = await fetch('https://api.pokemontcg.io/v2/cards?q=set.id:base1', {
mode: 'cors',
headers: {
'X-Api-key': '22173ccf-376e-4d12-8d50-8b25df6a8e13',
},
})
.then((res) => res.json())
.then((data) => {
console.log(data.data);
setCards(data.data);
console.log(cards.length);
})
.then(() => {
updateDeck(getRandom(cards, 4));
});
}
// }
fetchCards()
}, []);