在React Native中使用async / await时出错 [英] Error using async/await in React Native

查看:1113
本文介绍了在React Native中使用async / await时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当试图在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆