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

184
Views
How to get data with async func?

Ok, I have async function, which get data about cryptocurrency. And when I load page at first time, data is undefined. But, if I change something in code (for example: set insignificant Enter) data is appears.

How change my code to get data with statr page?

const getData = async () => {
    try {
      const resCoins = []
      for (let coinName of coinsName){
        let coin = await axios.get('https://api.coingecko.com/api/v3/coins/' + coinName)
        resCoins.push(coin.data)
      }

      let usd = {symbol: 'usd', market_data: {current_price: {}}}
      for (let currency of resCoins){
        usd.market_data.current_price[currency.id] =  1 / currency.market_data.current_price.usd
      }
      resCoins.push(usd)

      setCoins(resCoins);
    } catch (error) {
      console.error(error);
    }
  };
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

For is a sync operation. They will not wait for response. So you need to use Promise.all()

const resCoins = await Promise.all(
  coinsName.map(
    (coinName) =>
      axios
        .get("https://api.coingecko.com/api/v3/coins/" + coinName)
        .then((res) => res.data)
  )
)

More Info

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!