Skip to content

Instantly share code, notes, and snippets.

@spences10
Last active July 13, 2017 05:23
Show Gist options
  • Save spences10/aa49fbd75751661f929231107149cdc6 to your computer and use it in GitHub Desktop.
Save spences10/aa49fbd75751661f929231107149cdc6 to your computer and use it in GitHub Desktop.
JavaScript ES6 standard for getting json data from a URL in this case a Gist with famous Arnold Schwarzenegger film quotes
// Get random quote
document.getElementById("newQuote").onclick = getQuotes;
function fetchQuotes(callback) {
// arnold json gist
const endpoint =
'https://gist.githubusercontent.com/spences10/ceee092f6fed36559036c94682b7a5f7/raw/7a27570759834ee454ee380ca42ebd47dc55e932/arnold_quotes.json'
// fetch gist
fetch(endpoint)
.then(blob => blob.json())
.then(data => callback(data));
}
// callback for quotes promise
function getQuotes() {
fetchQuotes(function (quotes) {
// get random quote from JSON
const quoteID = getRandomInt(1, 100);
// get json data
var quoteStr = quotes[quoteID].quote
var movieStr = quotes[quoteID].movie
var characterStr = quotes[quoteID].character
alert(quoteStr);
})
}
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment