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

311
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

0

userdata is an async function. Try this:

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