admin管理员组文章数量:1295847
my problem is i want to get a json from this function but all i get is a only a promise and why i choose this way because in app that i'm working on it works dynamicly but the only place that i see i can put this promise is render()
this is my code :
var userInfo = (async()=>{
var value = await AsyncStorage.getItem("@user_info");
value = JSON.parse(value);
return value ;
})();
and this is my result :
Promise {
"_40": 0,
"_55": null,
"_65": 0,
"_72": null,
}
but what i want to get is a json what sould i have to do ?
my problem is i want to get a json from this function but all i get is a only a promise and why i choose this way because in app that i'm working on it works dynamicly but the only place that i see i can put this promise is render()
this is my code :
var userInfo = (async()=>{
var value = await AsyncStorage.getItem("@user_info");
value = JSON.parse(value);
return value ;
})();
and this is my result :
Promise {
"_40": 0,
"_55": null,
"_65": 0,
"_72": null,
}
but what i want to get is a json what sould i have to do ?
Share Improve this question asked Nov 13, 2017 at 21:43 shahabvshahabishahabvshahabi 9552 gold badges7 silver badges18 bronze badges3 Answers
Reset to default 3You have to call this function from ponentDidMount
and after the promise pletes, call setState
.
Here is a canonical example of how to do this:
class User extends React.Component {
state = { user: null };
render() {
return this.state.user && <div>{this.state.user.name}</div>;
}
async ponentDidMount() {
const value = await AsyncStorage.getItem("@user_info");
if (!this._unmounted) {
const user = JSON.parse(value);
this.setState({ user: user });
}
}
ponentWillUnmount() {
this._unmounted = true;
}
}
You should return value.json() . Try this
var userInfo = (async()=>{
var value = await AsyncStorage.getItem("@user_info");
return value.json() ;
})();
In addition, AsyncStorage.getItem is an asynchronous function, you will need to wrap that in try and catch in case if any error happened during the process
Note: If you use an await inside a function that is not explicitly declared with async, you'll end up with an Unexpected token syntax error.
async userInfo(){
try{
var value = await AsyncStorage.getItem("@user_info");
return value.json() ;
}
catch(e){
console.log('caught error', e);
// Handle exceptions
}
}
I think it is because your function is self-executing.
You should be able to access the value if you remove the self-execution
var userInfo = async () => {
var value = await AsyncStorage.getItem("@user_info");
value = JSON.parse(value);
return value ;
});
As AsyncStore.getItem
is a Promise you could also use .then
method
const userInfo = () => {
AsyncStorage.getItem("@user_info").then((response) => {
console.log(response);
});
}
本文标签: javascriptreact native how to get return from a promiseStack Overflow
版权声明:本文标题:javascript - react native how to get return from a promise - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741618705a2388680.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论