如何在 React Native 上使用 await 关键字? [英] How to use await key word on react native?
本文介绍了如何在 React Native 上使用 await 关键字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试在 React Native 上使用 await/async,但出现意外令牌错误.
I tried to use await/async on react native,but I got unexpected token error.
我在我的 javascript 源文件中添加了代码:
I added the code blow in my javascript source file:
var value = await AsyncStorage.getItem('STORAGE_KEY');
我的 React Native 版本是 0.15.0.
My react native version is 0.15.0.
我需要添加一些配置来使用 async/await 吗?
Do I need to add some configuration to use async/await?
推荐答案
我在我的 React Native 应用程序中使用了 async/await,并且不需要做任何事情来在我的项目中配置或启用它.我的用法是...
I'm using async/await in my react native app and didn't have to do anything to configure or enable it in my project. My usage takes the form of...
async getCache(key){
try{
let value = await AsyncStorage.getItem(key);
return value.json();
}
catch(e){
console.log('caught error', e);
// Handle exceptions
}
}
注意:如果您在未使用 async 显式声明的函数中使用 await,您最终会遇到意外令牌语法错误.
这篇关于如何在 React Native 上使用 await 关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文