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

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

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