Last active
July 13, 2017 05:23
-
-
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
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
// 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