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

90
Views
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 answers
Answer question

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