How to use await key word on react native?

Solution 1:

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
    }

}

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.

Solution 2:

Do I need to add some configuration to use async/await?

Yes. async is still not a finalized part of the specification; they're currently considered "Stage 3" (Candidate).

You can enable all Stage 3 language proposals in your .babelrc by using the stage-3 preset. Alternatively, you can add just the async plugin.