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

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

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