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

306
Views
Trying to put AsyncStorage.getItem() inside async/await try/catch nest but returning unexpected stuff

I have this on React-Native, basically I successfully saved some stuff using AsyncStorage from the LoginScreen. Now I'm trying to access those variables on my HomeScreen using AsyncStorage.getItem

const userdata = async () => { 
        try {
            return await AsyncStorage.getItem("user_nicename")      
        } catch(error) {
            console.log(error)
        }
    }

    console.log(userdata)
    //returns  { ƒ _callee() {
    //return _babel_runtime_regenerator__WEBPACK_IMPORTED_MODULE_0___default.a.async(function _callee$(_context) {
    //    while (1) {
    //      switch (_context.prev = _context.next) {....

What am I doing wrong here? It seems to be very straightforward.

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

0

There are a couple of things you are going wrong about this

  • You are logging the function without calling, if you want to see the function evaluated, you have to call it by adding parenthesis for example in your case it would userdata()
  • You are calling an asynchronous function as though it was a synchronous function. Your code was written asynchronously so you need to call it asynchronously as well.
  • As per js convention for naming identifier, turn userdata to userData

For your solution, you will need to call your function, userdata in a promise-based function either async-await or .then or in a callback function. I am suggesting two ways to improve your code.

Method 1: Using .then()

// in home screen
userdata()
    .then((data) => console.log(data))
    .catch((err) => console.log(err));

Method 2: using async-await in your home screen

// home screen
const getUserData = async () => {
   try {
     console.log(await userdata());
   } catch (err) {
     console.log(err);
   }
};

// then call the function
getUserData()

about 4 years ago · Juan Pablo Isaza Report

0

userdata is an async function. Try this:

console.log(await userdata())
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!