在React Native中使用async / await时出错 [英] Error using async/await in React Native
问题描述
当试图在react-native中使用async / await时,我收到以下错误:
When trying to use async/await in react-native, I am getting the following error:
uncaught error Error: SyntaxError: /Users/senthilsivanath/Documents/MusicTulip/index.ios.js: Unexpected token (50:23)
48 | renderScene: function(route,nav) {
49 | try {
50 | const response = await signIn.isLoggedIn();
我的 .babelrc
文件是:
{ "presets": ["react-native", "es2015", "babel-preset-stage-3"] }
推荐答案
你可能只是错过了<$ c第48行$ c> async 关键字。
You might just be missing the async
keyword on line 48.
更新代码以使用 async
关键字之前的关键字
关键字:
Update your code to use the async
keyword before the function
keyword:
renderScene: async function(route, nav) {
try {
const response = await signIn.isLoggedIn();
// ...
或者当使用箭头功能时,请输入 async
参数列表前的关键字:
Or when using an arrow function, put the async
keyword before the parameter list:
renderScene: async (route, nav) => {
try {
const response = await signIn.isLoggedIn();
在JavaScript中, async
关键字是装饰器警告运行时连接的机箱将使用等待
关键字,因此您始终可以看到它们一起使用。这就是为什么你会听到人们将这种语法称为 async / await
语法。
In JavaScript, the async
keyword is a decorator that warns the runtime that the attached enclosure will use the await
keyword, so you always see them used together. Which is why you will hear people refer to this syntax as the async/await
syntax.
简单put:如果没有 async
,你不能使用 await
。
Simply put: You can't use await
without async
.
编辑:如果你在课堂上声明这个,那么请确保你的语法正确:
If you are declaring this inside of a class, then just be sure that your syntax is correct:
class MusicTulip extends Component {
async renderContent() {
const response = await signIn.isLoggedIn();
}
}
希望这有帮助!
这篇关于在React Native中使用async / await时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!