简体   繁体   中英

Getting actual value from react-native asyncStorage instead of promise

All morning I've been trying to get the AsyncStorage.getItem to return the value of the actual item, not a promise.

Here's what I have that according to everything I've read should work:

export async function getToken(){

    try{
        var value = await AsyncStorage.getItem('authToken');
        return value;
    }catch (error){
        console.log(error);
    }

}   

I have tried chaining together many .thens, tried accessing the actual value field of the promise, but for some reason that returns a promise also. I've tried pretty much every suggestion on this site and it will always return a promise.

Quoting the AsyncStorage documentation

... Each method in the API returns a Promise object.

and also based on this post , there is no way for you to access the value without the use of promise or any other async patterns (generators, callback etc).

You can use Realm as a storage without using promises. This is how you query data in realm (quoting the documentation ):

 let token = realm.objects('authToken'); // retrieves all authTokens 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM