Created
September 7, 2017 01:18
-
-
Save P1xt/6b07773778deae4c14b75fe8315f11a3 to your computer and use it in GitHub Desktop.
Async/Await with fetch to retrieve quotes from an API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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