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
In my code I got undefined while function returns true

Function Definition

export const checkAuth = (accessToken) => {
const url = 'https://example/user-profile';
    console.log("We are in outer function");
    fetch(url, {
        method: 'GET',
        headers: new Headers({
            'Authorization' : 'Bearer ' + accessToken,
        })
    }).then(res => res.json())
        .catch(function(error){
            console.error('Error : ', error)
        })

    .then((response) => {
            if(response.error != 'Unauthorized')
            {
                return true;
            }
            else{
                return false;
            }
    })
}

Function Calling

var res=checkAuth(this.state.accessToken);

In the res variable, I got undefined but I am passing true. How can I solve the problem?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The checkAuth function isn't returning a value from the function. You are returning a value from the Promise chain, but that is the value that should be returned from checkAuth. You can simply return the Promise that fetch returns.

export const checkAuth = (accessToken) => {
  const url = 'https://example/user-profile';
  console.log("We are in outer function");
  return fetch(url, {
    method: 'GET',
    headers: new Headers({
      'Authorization' : 'Bearer ' + accessToken,
    })
  }).then(res => res.json())
    .catch(function(error){
      console.error('Error : ', error)
    })
    .then((response) => {
      return response.error != 'Unauthorized'
    });
}

Because checkAuth now returns a Promise you will need to either call it in an async function and await the Promise to resolve, or chain from the returned Promise.

checkAuth(this.state.accessToken)
  .then(value => {
    // do something with value
  })
  .catch(error => {
    // handle any Promise rejections if they occur
  });
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!