Last active
November 1, 2016 13:45
-
-
Save m-muhsin/9f3b86bf96e059d4c3cbd94d5089b92f to your computer and use it in GitHub Desktop.
A meat JS Promises example
This file contains 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>JS Promises</title> | |
<meta charset="UTF-8"> | |
</head> | |
<body style="padding: 20px;"> | |
I want | |
<input type="text" name="count" id="count"> | |
<input type="button" value="meaty sentences" id="btnGet" onclick="getSentences()"> | |
<ol id="container"></ol> | |
<script> | |
var btnGet = document.getElementById("btnGet"); | |
function getSentences(){ | |
var count = document.getElementById("count").value; | |
var container = document.getElementById("container"); | |
container.innerHTML = ''; | |
var url = 'http://baconipsum.com/api/?type=all-meat&sentences='+count; | |
fetch(url) | |
.then(results => results.json()) | |
.then(array => array[0]) | |
.then(text => text.split('. ')) | |
.then(array => array.forEach( | |
sentence => container.innerHTML = container.innerHTML + '<li>' + sentence + '</li>')) | |
.catch(function(err) { | |
console.log("Didn't connect to the API", err) | |
}); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment