Skip to content

Instantly share code, notes, and snippets.

@P1xt
Created September 7, 2017 01:18
Show Gist options
  • Save P1xt/6b07773778deae4c14b75fe8315f11a3 to your computer and use it in GitHub Desktop.
Save P1xt/6b07773778deae4c14b75fe8315f11a3 to your computer and use it in GitHub Desktop.
Async/Await with fetch to retrieve quotes from an API
async getQuote() {
const catagories = ['inspire', 'funny', 'students', 'life', 'management', 'sports'];
const randomCategory = Math.floor((Math.random() * (catagories.length - 1) + 0));
const response = await fetch(this.state.source + catagories[randomCategory]);
const data = await response.json();
this.setState({
quote: {
text: data.contents.quotes[0].quote,
author: data.contents.quotes[0].author,
twitterLink: this.tweetThis(data.contents.quotes[0].quote)
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment