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

289
Visualizações
How to get data from a promise (async/await) and use it `outside` the promise?

So I await for the promise to fulfil, then I want to use the resulting data outside of .then() or the async function.

function HomeScreen() {

    const userData = async () => { 
        await AsyncStorage.getItem("user_nicename")     
    }

    return (
        <View style={styles.container}>      
            <Text>Hello, { userData() } </Text>  //*I want to use it here for example*//

            <Button 
                style={styles.buttonLogin}
                onPress={() => navigation.navigate('LoginScreen')}
                title='Logout'
            />      
        </View>
    ) 
}

export default HomeScreen;

of course my code will throw an error because userData() is a 'promise object'. I just want the string value, which I have no problems accessing inside .then(data => ... ). But how do I access it outside so I can use it somewhere else?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You to use useState and useEffect

import { useState, useEffect } from "react";

function HomeScreen() {
    const [userData, setUserData] = useState(undefined);

    useEffect(() => {
        (async () => {
            setUserData(await AsyncStorage.getItem("user_nicename"));
        })();
    }, []);

    return (
        <View style={styles.container}>      
            <Text>Hello, { userData } </Text>

            <Button 
                style={styles.buttonLogin}
                onPress={() => navigation.navigate('LoginScreen')}
                title='Logout'
            />      
        </View>
    ) 
}

export default HomeScreen;
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