Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

119
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!