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

152
Views
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 answers
Answer question

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 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!