Skip to content

Instantly share code, notes, and snippets.

@BenFausch
Created March 5, 2019 21:46
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 BenFausch/d6344c9d9beec61ab709e8afdeee9890 to your computer and use it in GitHub Desktop.
Save BenFausch/d6344c9d9beec61ab709e8afdeee9890 to your computer and use it in GitHub Desktop.
Simplest implementation of a promise chain
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Promise Chain Example (supported by ALL browsers except IE11)</title>
<link rel="author" href="humans.txt">
</head>
<body>
<h1>CHECK Your console for messages!</h1>
<script>
function firstFunction() {
return new Promise(function(resolve, reject) {
setTimeout(function() {
console.log('Me First at 3 seconds!')
resolve('cars')
}, 3000);
});
}
function secondFunction() {
return new Promise(function(resolve, reject) {
firstFunction().then(function(response) {
setTimeout(function() {
console.log('Me Second at 6 seconds! Message from firstFunction: ' + response)
resolve(response+ ' are')
}, 3000);
})
})
}
function thirdFunction() {
return new Promise(function(resolve, reject) {
secondFunction().then(function(response) {
setTimeout(function() {
console.log('Me Third at 9 seconds! Message from secondFunction: ' + response)
resolve(response + ' cash money')
}, 3000);
})
})
}
function execute(){
thirdFunction().then(function(response){
setTimeout(function() {
console.log('Me last at 12 seconds! The third function says: ' + response)
},3000)
});
}
execute();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment