Advertisement
karlakmkj

Constructing a Promise Object

Dec 28th, 2020
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const inventory = {
  2.   sunglasses: 1900,
  3.   pants: 1088,
  4.   bags: 1344
  5. };
  6.  
  7. // Write your code below:
  8. const myExecutor = (resolve, reject) => {
  9.   if (inventory.sunglasses >0){
  10.     resolve('Sunglasses order processed.');
  11.   }
  12.   else {
  13.     reject('That item is sold out.');
  14.   }
  15. }
  16.  
  17. //Create a function, orderSunglasses(). This function should have no parameters.
  18. const orderSunglasses = () => {
  19.   return new Promise(myExecutor);
  20. }
  21.  
  22. const orderPromise = orderSunglasses();
  23. console.log(orderPromise);
  24.  
  25. /*
  26. Output will show:
  27. 1. if condition is true
  28. 2. if condition is false - sunglasses is zero
  29.  
  30. $ node app.js
  31. Promise { 'Sunglasses order processed.' }
  32. $ node app.js
  33. Promise { <rejected> 'That item is sold out.' }
  34. (node:1027) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): That item is sold out.
  35. (node:1027) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
  36. $
  37. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement