How to get AsyncStorage value from outside of function - react-native

I want to access my token from ACCESS_TOKEN . and merge to my URL.
async getToken(){
try{
var token = await AsyncStorage.getItem('ACCESS_TOKEN')
}catch(error){}
return token;
};
componentDidMount(){
var v = this.getToken()
Alert.alert(v)
return fetch('http://localhost/'+v);
}
I get the error in the output.
com.facebook.react.bridge.ReadableNativeMap cannot be casst to java.lang.string

You need the async and await in componentDidMount as well as getToken returns a promise (as with all functions declare with async).
async componentDidMount() {
let v = await this.getToken();

Related

A call to an async function is not awaited. Use the "await" keyword in Test Cafe

I'm getting this error:
A call to an async function is not awaited. Use the "await" keyword
before actions, assertions or chains of them to ensure that they run
in the right sequence.
But I am using an await within my code, what am I doing incorrectly?
test("Accepts the cookie", async t => {
const interfaceSelect = Selector('#sp_message_iframe_617100');
await
t.switchToIframe('#sp_message_iframe_617100')
t.click(Selector('button').withText('Accept'))
t.switchToIframe('#select_region-select-module_select_30IZf')
const countrySelect = Selector('#region-select-module_select__30lZf');
t.click(Selector('button').withText('United Kingdom'))
});
Thanks,
The TestCafe's methods are chainable and return a Promise.
You need to add an await before each method call or one await before the whole chain.
test("Accepts the cookie", async t => {
const interfaceSelect = Selector('#sp_message_iframe_617100');
await t.switchToIframe('#sp_message_iframe_617100');
await t.click(Selector('button').withText('Accept'));
await t.switchToIframe('#select_region-select-module_select_30IZf');
const countrySelect = Selector('#region-select-module_select__30lZf');
await t.click(Selector('button').withText('United Kingdom'));
});
test("Accepts the cookie", async t => {
const interfaceSelect = Selector('#sp_message_iframe_617100');
await t
.switchToIframe('#sp_message_iframe_617100')
.click(Selector('button').withText('Accept'))
.switchToIframe('#select_region-select-module_select_30IZf');
const countrySelect = Selector('#region-select-module_select__30lZf');
await t.click(Selector('button').withText('United Kingdom'));
});

Calling async function from custom flatlist view

I am trying to call an async function in a customer flatlist view, but I am get errors caused by using await in the custom flatlist view:
async _customFlatlistView(item){
promise = await SomeFunction();
promise2 = await SomeOtherFunction();
}
await can only be used in async functions
_customFlatlistView = async item => {
promise = await SomeFunction()
promise2 = await SomeOtherFunction()
}
The await keyword can only be used in adding the async keyword to the method signature:
async _customFlatlistView(item){
promise = await SomeFunction();
promise2 = await SomeOtherFunction();
}

await AsyncStorage return a promise instead value

I have an async function that calls AsyncStorage.getItem but always returns a promise.
I tried to use .then clause but the result is similar
I tried to use AsyncStorage.getItem out of the function but I get the error "await is a reserved word"
getDataStorage = async () => {
console.log("getDataStorage");
var data = '';
try {
data = await AsyncStorage.getItem('dataStorage');
console.log("getting data " + data);
return data;
} catch (error) {
console.log("----" + error.message);
}
};
componentDidMount(){
console.log("componentDidMount");
var data = this.getDataStorage();
console.log(data);
}
The result is first displays the promise then prints the value that I get with getItem().
I want to get the value, I suppose with await the function waits for the result of getItem, is it correct?
Yes, await functions wait for the result. But, in your case its only waiting till returning promise, so you have to change your code as:
componentDidMount = async () => {
console.log("componentDidMount");
data = await this.getDataStorage();
console.log(data);
}

Can't display from async function result

I need to display in my html variable: {my_dates}. The problem that I can't get it work with fetch but can do with old ajax request.
not working code:
created: function(){
//this.getTableData()
url = 'http://dlang.ru/test'
async function fetchAsync () {
const response = await fetch(url);
return await response.json();
}
this.my_dates = fetchAsync();
}
Working code:
$.ajax({
url: "http://dlang.ru/test",
success: function (data) {
Vue.set(app, "my_dates", data);
app.show = true;
}
});
If you want to be able to assign result of fetchAsync() to this.my_dates then entire created hook method needs to be declared as async.
Then you also need await in front of fetchAsync call:
created: async function () {
const url = 'http://dlang.ru/test';
async function fetchAsync() {
const response = await fetch(url);
return await response.json();
}
this.my_dates = await fetchAsync();
}
Please try this:
created: function(){
//this.getTableData()
url = 'http://dlang.ru/test'
async function fetchAsync () {
const response = await fetch(url);
const data = await response.json();
Vue.set(app, "my_dates", data);
app.show = true;
}
fetchAsync();
}

React Native - get data from Promise

in my React Native app I receive a token from an API. Everytime the app sends a request to the server this token is needed. I save the token in the AsyncStorage:
export const onSignIn = (value) => AsyncStorage.setItem('USER_TOKEN', value);
In many different parts of the app I need this token and therefore I wanted to use a function, that extracts the information out of the token:
export const getTokenInfo = async () => {
try{
const value = await AsyncStorage.getItem('USER_TOKEN')
.then((res) => {
const jsonData = jwtDecode(res);
return jsonData;
})
}
catch(e){
console.log('caught error', e);
}
}
When calling the function in other Components it just returns the Promise itself and not the token. Is there a possibility to get the token, but not the promise? A possible approach was to use setState() to store the token in a state, but there are some components like DrawerNavigator that are not in a class.
Thanks!
Your forgot to return the value on your getTokeninfo function
export const getTokenInfo = async () => {
try{
const value = await AsyncStorage.getItem('USER_TOKEN')
.then((res) => {
const jsonData = jwtDecode(res);
return jsonData;
})
return value // <---- you forgot this line
}
catch(e){
console.log('caught error', e);
}
}