Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

95
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda