Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width">
- <title>JS Bin</title>
- </head>
- <body>
- <script id="jsbin-javascript">
- /* ES7 */
- const isMomHappy = true;
- // Promise
- const willIGetNewPhone = new Promise(
- (resolve, reject) => {
- if (isMomHappy) {
- const phone = {
- brand: 'Samsung',
- color: 'black'
- };
- resolve(phone);
- } else {
- const reason = new Error('mom is not happy');
- reject(reason);
- }
- }
- );
- // 2nd promise
- async function showOff(phone) {
- return new Promise(
- (resolve, reject) => {
- var message = 'Hey friend, I have a new ' +
- phone.color + ' ' + phone.brand + ' phone';
- resolve(message);
- }
- );
- };
- // call our promise
- async function askMom() {
- try {
- console.log('before asking Mom');
- let phone = await willIGetNewPhone;
- let message = await showOff(phone);
- console.log(message);
- console.log('after asking mom');
- }
- catch (error) {
- console.log(error.message);
- }
- }
- (async () => {
- await askMom();
- })();
- </script>
- <script id="jsbin-source-javascript" type="text/javascript">/* ES7 */
- const isMomHappy = true;
- // Promise
- const willIGetNewPhone = new Promise(
- (resolve, reject) => {
- if (isMomHappy) {
- const phone = {
- brand: 'Samsung',
- color: 'black'
- };
- resolve(phone);
- } else {
- const reason = new Error('mom is not happy');
- reject(reason);
- }
- }
- );
- // 2nd promise
- async function showOff(phone) {
- return new Promise(
- (resolve, reject) => {
- var message = 'Hey friend, I have a new ' +
- phone.color + ' ' + phone.brand + ' phone';
- resolve(message);
- }
- );
- };
- // call our promise
- async function askMom() {
- try {
- console.log('before asking Mom');
- let phone = await willIGetNewPhone;
- let message = await showOff(phone);
- console.log(message);
- console.log('after asking mom');
- }
- catch (error) {
- console.log(error.message);
- }
- }
- (async () => {
- await askMom();
- })();</script></body>
- </html>
Add Comment
Please, Sign In to add comment