How can I change the following code to async syntax?
componentWillMount(){
fetch('http://localhost:9000/api/homes')
.then( response => {
if(response.ok) {
return response.json();
}
throw new Error('Network response was not ok.');
})
.then( data => {
this.setState({
originalList:data,
displayedList: data
});
}
)
.catch( error => {
console.error(`fetch operation failed: ${error.message}`);
});
}
I have tried to do it like this:
componentWillMount(){
const res = await fetch('http://localhost:9000/api/homes');
const json = await res.json;
this.setState({
originalList:json,
displayedList: json
});
}
and I get the following error: Syntax error: C:/Users/EYAL/web/fe/react/airbnb/src/Homes/index.js: await is a reserved word (17:14)