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

221
Views
Await is not as i expected in if else statements
let metadata = [];

    allNFTs.map(async (e) => {
      if (e.metadata) {
        metadata.push(JSON.parse(e.metadata).attributes);
      } else {
        let config = {
          method: "get",
          url: `http://localhost:3000/api/fetch`,
          header: {
            "Content-Type": "application/json",
          },
        };
        const res = await axios(config);
        const attr = res.data.attributes;
        metadata.push(attr);
        console.log(metadata); // this one worked after below
      }
    });

    console.log(metadata); // this one worked before above

enter image description here

But i want to wait till my axios done fetching, so i can finally console.log that my actual metadata.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Make an array of promises, and then await them with Promise.all

const metadataPromises = allNFTs.map((e) => {
  if (e.metadata) {
    return Promise.resolve(JSON.parse(e.metadata).attributes);
  } else {
    let config = {
      method: "get",
      url: `http://localhost:3000/api/fetch`,
      header: {
        "Content-Type": "application/json",
      },
    };
    return axios(config).then((res) => res.data.attributes);
  }
});

// await still has to be in an async function
const metadata = await Promise.all(metadataPromises);

console.log(metadata);

// or use .then
Promise.all(metadataPromises).then((metadata) => console.log(metadata));
about 4 years ago · Juan Pablo Isaza Report

0

The issue with you code, that you don't wait. The latest console.log is execurted before the map iterating all the items.

You should use somehting like that: https://www.npmjs.com/package/modern-async E.g.

async function init() {
   var ma= require('modern-async')
   await ma.mapSeries(allNFTs,async (e)=>{
   if (e.metadata) {
        metadata.push(JSON.parse(e.metadata).attributes);
      } else {
        let config = {
          method: "get",
          url: `http://localhost:3000/api/fetch`,
          header: {
            "Content-Type": "application/json",
          },
        };
        const res = await axios(config);
        const attr = res.data.attributes;
        metadata.push(attr);
        console.log(metadata);
      }
   })
   console.log(metadata);

}
about 4 years ago · Juan Pablo Isaza Report
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!