Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

120
Visualizações
javascript fetch inside foreach loop to pushing array

When i load the page for the first time, i execute these instructions

var arrayObj = [];  
var marketItems = [];
  
marketItems.push({
    "tokenId": "18",
    "tokenAddress": "0x19d66326c89618e101e106a36710ad4614e15865",
    "tokenUri": "https://uri1...",
});
marketItems.push({
    "tokenId": "19",
    "tokenAddress": "0x19d66326c89618e101e106a36710ad4614e15865",
    "tokenUri": "https://uri2...",
});    

marketItems.forEach(async item => {

  var obj = await fetch(item.tokenUri)
  .then(response => response.json())
  .then(data => {
    obj = { "dataTokenUid": data.uid, "dataTokenId": item.tokenId, "dataTokenAddress": item.tokenAddress, "dataName": data.name, "dataDescription": data.description };
    arrayObj.push(obj);      
  });

});

console.log(arrayObj)

in the first log the array is empty, but without reloading the page if i call the following function i get the right log ..

function callItem () {
   console.log("Arrai costruito");
   console.log(arrayObj);
}

Arrai costruito 
Array [ {…}, {…} ]
0: Object { dataTokenUid: "9", dataTokenId: "18", dataTokenAddress: "0x19d66326c89618e101e106a36710ad4614e15865", … }
1: Object { dataTokenUid: "10", dataTokenId: "19", dataTokenAddress: "0x19d66326c89618e101e106a36710ad4614e15865", … }
length: 2

there is obviously a synchronization problem that i have not been able to solve, i would like to see the array in the log when i load the page, what can I do?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda