I am currently working with a cardsList
object that is retrieved from a fetch()
function. As I iterate over the cardsList
and make additional requests to get more information for each card, I have encountered a peculiar issue. Despite the mapping process being synchronous, the console output shows 'Log2' before 'Log1'.
Strangely, although I can view all the information of the cardInfo
objects when I print cardsList
, attempting to access it with syntax like cardsList[0].cardInfo
returns undefined.
Have you encountered a similar problem before? Any insights into what might be causing this behavior?
*Note: I have experimented with using await
in fetchCardsInfo
, but the issue persists. I can see the data when I log it, but accessing it seems to be problematic.
buscarCartoes = async () => {
let cardsList = await CodeConnectRequests.fetchCardsList()
cardsList.map((card) => {
const cardInfo = CodeConnectRequests.fetchCardsInfo(card.cartao.tkCartao)
cardInfo.then(data=>{
console.log('Log1')
card['cardInfo'] = data
})
return card
})
console.log('Log2')
console.log(cardsList)// Here I can see cardInfo infs
console.log(cardsList[0].cardInfo)// But hete cardInfo will be undefined
}