Guest User

Untitled

a guest
Aug 16th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width">
  6. <title>JS Bin</title>
  7. </head>
  8. <body>
  9.  
  10. <script id="jsbin-javascript">
  11. /* ES7 */
  12. const isMomHappy = true;
  13.  
  14. // Promise
  15. const willIGetNewPhone = new Promise(
  16. (resolve, reject) => {
  17. if (isMomHappy) {
  18. const phone = {
  19. brand: 'Samsung',
  20. color: 'black'
  21. };
  22. resolve(phone);
  23. } else {
  24. const reason = new Error('mom is not happy');
  25. reject(reason);
  26. }
  27.  
  28. }
  29. );
  30.  
  31. // 2nd promise
  32. async function showOff(phone) {
  33. return new Promise(
  34. (resolve, reject) => {
  35. var message = 'Hey friend, I have a new ' +
  36. phone.color + ' ' + phone.brand + ' phone';
  37.  
  38. resolve(message);
  39. }
  40. );
  41. };
  42.  
  43. // call our promise
  44. async function askMom() {
  45. try {
  46. console.log('before asking Mom');
  47.  
  48. let phone = await willIGetNewPhone;
  49. let message = await showOff(phone);
  50.  
  51. console.log(message);
  52. console.log('after asking mom');
  53. }
  54. catch (error) {
  55. console.log(error.message);
  56. }
  57. }
  58.  
  59. (async () => {
  60. await askMom();
  61. })();
  62. </script>
  63.  
  64.  
  65.  
  66. <script id="jsbin-source-javascript" type="text/javascript">/* ES7 */
  67. const isMomHappy = true;
  68.  
  69. // Promise
  70. const willIGetNewPhone = new Promise(
  71. (resolve, reject) => {
  72. if (isMomHappy) {
  73. const phone = {
  74. brand: 'Samsung',
  75. color: 'black'
  76. };
  77. resolve(phone);
  78. } else {
  79. const reason = new Error('mom is not happy');
  80. reject(reason);
  81. }
  82.  
  83. }
  84. );
  85.  
  86. // 2nd promise
  87. async function showOff(phone) {
  88. return new Promise(
  89. (resolve, reject) => {
  90. var message = 'Hey friend, I have a new ' +
  91. phone.color + ' ' + phone.brand + ' phone';
  92.  
  93. resolve(message);
  94. }
  95. );
  96. };
  97.  
  98. // call our promise
  99. async function askMom() {
  100. try {
  101. console.log('before asking Mom');
  102.  
  103. let phone = await willIGetNewPhone;
  104. let message = await showOff(phone);
  105.  
  106. console.log(message);
  107. console.log('after asking mom');
  108. }
  109. catch (error) {
  110. console.log(error.message);
  111. }
  112. }
  113.  
  114. (async () => {
  115. await askMom();
  116. })();</script></body>
  117. </html>
Add Comment
Please, Sign In to add comment