O'Reilly logo

React Native By Example by Richard Kho

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using the Async and Await keywords

Normally, to deal with an asynchronous function, we would chain some promises to it in order to grab our data. We can write _updateList, like this:

_updateList () {   AsyncStorage.getItem('listOfTasks');     .then((response) => {fto       return JSON.parse(response);     })     .then((parsedResponse) => {       this.setState({         listOfTasks: parsedResponse       });     }); } 

However, this can become quite complicated. Instead, we will use the async and await keywords to create a simpler solution:

async _updateList () {   let response = await AsyncStorage.getItem('listOfTasks');   let listOfTasks = await JSON.parse(response) || [];   this.setState({     listOfTasks   });   this._changeTextInputValue(''); } 

The async keyword in front of ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required