Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

229
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda