fueanta

JavaScript Promise Demo

Apr 7th, 2020
294
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Promise Demonstration
  2. const asyncProblemSolver = isResolved => {
  3.     return new Promise((resolve, reject) => {
  4.         if (isResolved) {
  5.             resolve("Problem is resolved!")
  6.         } else {
  7.             reject("Problem could not be resolved, sorry!")
  8.         }
  9.     })
  10. }
  11.  
  12. console.log("Some syncronous task 01.")
  13.  
  14. asyncProblemSolver(false).then(success => {
  15.     return new Promise(resolve => {
  16.         resolve(success + " Just double checked the solution.")
  17.     })
  18. }).then(success => {
  19.     console.log(success)
  20. }).catch(error => {
  21.     console.log(error)
  22. })
  23.  
  24. console.log("Some syncronous task 02.")
  25.  
  26. setTimeout(() => {
  27.     console.log("Some syncronous task 03.")
  28.     console.log("Some syncronous task 04.")
  29. }, 1)
RAW Paste Data