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

144
Vistas
Trying to execute an imported Async function but the function is not behaving asynchronously

I am using React to build a website. I have imported an asynchronous function to execute when I press a button. However, the function is not working asynchronously and I really don't understand why.

interact.js:

export const getNFT = async () => {
  setTimeout(() => {
    console.log('getNFT code execute');
    return nft;
  }, 2000);
};

const nft = {
tokenURI: 'https://gateway.pinata.cloud/ipfs/QmdxQFWzBJmtSvrJXp75UNUaoVMDH49g43WsL1YEyb',
imageURL: 'https://gateway.pinata.cloud/ipfs/QmeMTHnqdfpUcRVJBRJ4GQ2XHU2ruVrdJqZhLz',
ID: '212'
};

Main.js

import {
  getNFT
} from 'interact.js';


// This function is executed when a user clicks on a button
let getAllocatedNFT = async () => {
try {
  let response = await getNFT();
  console.log('response from server:: '+response);
}catch(e){
  console.log(e);
}
};

console:

response from server:: undefined
getNFT code execute // This is executed correctly after 2 seconds
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You have to return promise which will resolve your webAPI(setTimeout) Please use like below:

  const getNFT = async () => {
    return new Promise(resolve => setTimeout(() => {
        console.log("getNFT code execute")
        resolve(true)
      }, 2000)
    );
  };
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