• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

150
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 3 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 3 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 Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error