Skip to content

Instantly share code, notes, and snippets.

@ivosh
Created February 12, 2018 09:11
Show Gist options
  • Save ivosh/fa8c11f32c7272e156166f9f9f47d139 to your computer and use it in GitHub Desktop.
Save ivosh/fa8c11f32c7272e156166f9f9f47d139 to your computer and use it in GitHub Desktop.
evolving-react-patterns-async-await
componentDidMount() {
this.setState({ content: this.props.loading() })
try {
const res = await fetch(this.props.url);
this.setState({ content: this.props.done(res.json()) });
} catch (err) {
this.setState({ content: this.props.error() });
}
}
@harkinj
Copy link

harkinj commented Jan 3, 2019

I couldn't get your gist working but this seems to work :
async componentDidMount() {
this.setState({ content: this.props.loading() })

try {
  const res = await fetch(this.props.url);
  this.setState({ content: this.props.done(await res.json()) });
} catch (err) {
  this.setState({ content: this.props.error() });
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment