Created
December 19, 2017 06:32
-
-
Save mhackersu/c0bae41f79d8dcf2edc4139ffa45b3c2 to your computer and use it in GitHub Desktop.
Promise with Callback
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> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/bluebird/1.2.2/bluebird.js"></script> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
/* ES5, using Bluebird */ | |
var isMomHappy = true; | |
// Promise | |
var willIGetNewPhone = new Promise( | |
function (resolve, reject) { | |
if (isMomHappy) { | |
var phone = { | |
brand: 'Samsung', | |
color: 'black' | |
}; | |
resolve(phone); | |
} else { | |
var reason = new Error('mom is not happy'); | |
reject(reason); | |
} | |
} | |
); | |
// call our promise | |
var askMom = function () { | |
willIGetNewPhone | |
.then(function (fulfilled) { | |
// yay, you got a new phone | |
console.log(fulfilled); | |
}) | |
.catch(function (error) { | |
// ops, mom don't buy it | |
console.log(error.message); | |
}); | |
} | |
askMom(); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">/* ES5, using Bluebird */ | |
var isMomHappy = true; | |
// Promise | |
var willIGetNewPhone = new Promise( | |
function (resolve, reject) { | |
if (isMomHappy) { | |
var phone = { | |
brand: 'Samsung', | |
color: 'black' | |
}; | |
resolve(phone); | |
} else { | |
var reason = new Error('mom is not happy'); | |
reject(reason); | |
} | |
} | |
); | |
// call our promise | |
var askMom = function () { | |
willIGetNewPhone | |
.then(function (fulfilled) { | |
// yay, you got a new phone | |
console.log(fulfilled); | |
}) | |
.catch(function (error) { | |
// ops, mom don't buy it | |
console.log(error.message); | |
}); | |
} | |
askMom();</script></body> | |
</html> |
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
/* ES5, using Bluebird */ | |
var isMomHappy = true; | |
// Promise | |
var willIGetNewPhone = new Promise( | |
function (resolve, reject) { | |
if (isMomHappy) { | |
var phone = { | |
brand: 'Samsung', | |
color: 'black' | |
}; | |
resolve(phone); | |
} else { | |
var reason = new Error('mom is not happy'); | |
reject(reason); | |
} | |
} | |
); | |
// call our promise | |
var askMom = function () { | |
willIGetNewPhone | |
.then(function (fulfilled) { | |
// yay, you got a new phone | |
console.log(fulfilled); | |
}) | |
.catch(function (error) { | |
// ops, mom don't buy it | |
console.log(error.message); | |
}); | |
} | |
askMom(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment