Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
320 views
in Technique[技术] by (71.8m points)

javascript - Error using async/await in React Native

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();

My .babelrc file is:

{ "presets": ["react-native", "es2015", "babel-preset-stage-3"] }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You might just be missing the async keyword on line 48.

Update your code to use the async keyword before the function keyword:

renderScene: async function(route, nav) {
    try {
        const response = await signIn.isLoggedIn();
        // ...

Or when using an arrow function, put the async keyword before the parameter list:

 renderScene: async (route, nav) => {
        try {
            const response = await signIn.isLoggedIn();

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.

Simply put: You can't use await without async.

Edit: 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();
    }
 }

Hope this helps!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...