Skip to content

Instantly share code, notes, and snippets.

@m-muhsin
Last active November 1, 2016 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m-muhsin/9f3b86bf96e059d4c3cbd94d5089b92f to your computer and use it in GitHub Desktop.
Save m-muhsin/9f3b86bf96e059d4c3cbd94d5089b92f to your computer and use it in GitHub Desktop.
A meat JS Promises example
<!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