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

93
Vistas
Return value from function React from an Axios' response

I have this function in React that should export the result got from an Axios' API call:

import axios from 'axios'

export function youtube(channelId) {

    var config = {
    method: 'get',
    url: `https://www.googleapis.com/youtube/v3/channels?part=statistics&id=${channelId}&key=AIzaxXXXXXXXXXXXXXXXXXXXXXX`,
    headers: { }
    };

    axios(config)
    .then(function (response) {
        console.log(response.data.items[0].statistics.subscriberCount)
        return response.data.items[0].statistics.subscriberCount
    })
    .catch(function (error) {
        return 0
    });
    

} 

Problem: it returns undefined. But the console.log displays the correct data.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

In a Synchronous way that is not possible but yes in an asynchronous way, it's possible, As per our comment discussion, you are accessing the promise and for asynchronous tasks, you need to resolve it to return any value or use async/await for better reading purposes.

In your code, the problem is you are returning in then/catch, another then/catch will be waiting for your input, it will not succeed until you will not settled the promise.

Codesandbox: https://codesandbox.io/s/divine-cherry-dqu40?file=/src/App.js

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