Advertisement
karlakmkj

async function

Dec 29th, 2020
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Using function for promise
  2. function withConstructor(num){
  3.   return new Promise((resolve, reject) => {
  4.     if (num === 0){
  5.       resolve('zero');
  6.     } else {
  7.       resolve('not zero');
  8.     }
  9.   })
  10. }
  11.  
  12. withConstructor(0)
  13.   .then((resolveValue) => {
  14.   console.log(` withConstructor(0) returned a promise which resolved to: ${resolveValue}.`);
  15. })
  16.  
  17. // Write your code below:
  18. //Using async function that will always return a promise (hence do not use the new keyword)
  19. //Write async function to reproduce the functionality of withConstructor() above
  20. async function withAsync(num){
  21.     if (num === 0){
  22.       return 'zero';
  23.     } else {
  24.       return 'not zero';
  25.     }
  26. }
  27.  
  28. withAsync(100)
  29.   .then((resolveValue) => {
  30.   console.log(` withAsync(100) returned a promise which resolved to: ${resolveValue}.`);
  31. })
  32.  
  33. /*
  34. Output will show:
  35. $ node app.js
  36.  withConstructor(0) returned a promise which resolved to: zero.
  37.  withAsync(100) returned a promise which resolved to: not zero.
  38. $
  39. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement