Advertisement
samimwebdev

asynchronous progreamming

Oct 24th, 2021
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Top to bottom (code read)
  2.  
  3. //Instant (synchronous)
  4. //Not Instant (asynchronous)
  5. // API call
  6. // setTimeOut (our own)
  7. //file reading/file write , database data store, data fetch
  8.  
  9. //asynchronous code dealing
  10. //callback
  11. //promise
  12. //Async await
  13.  
  14. // function getOne() {
  15. //   return 1
  16. // }
  17.  
  18. // function getTwo() {
  19. //   return 2
  20. // }
  21.  
  22. // function getThree(callback) {
  23. //   setTimeout(() => {
  24. //     callback(3)
  25. //   }, 2000)
  26. // }
  27.  
  28. // function getFour(number) {
  29. //   return number
  30. // }
  31.  
  32. // console.log(getOne())
  33. // console.log(getTwo())
  34.  
  35.  
  36. //   getThree(function (data) {
  37. //     console.log(data) //result is available
  38. //     //getFour()
  39. //    // console.log(getFour(data + 1))  //Dependency
  40. //   })
  41.  
  42. // console.log(getFour())
  43.  
  44. //promise pattern
  45. function getOne() {
  46.   return 1
  47. }
  48.  
  49. function getTwo() {
  50.   return 2
  51. }
  52.  
  53. function getThree(isResolved) {
  54.   return new Promise((resolve, reject) => {
  55.     setTimeout(() => {
  56.       if (isResolved) {
  57.         resolve(3)
  58.       } else {
  59.         reject(new Error(3))
  60.       }
  61.     }, 2000)
  62.   })
  63. }
  64.  
  65. function getFour(number) {
  66.   return new Promise((resolve, reject) => {
  67.     setTimeout(() => {
  68.       resolve(number)
  69.     }, 2000)
  70.   })
  71. }
  72.  
  73. console.log(getOne())
  74. console.log(getTwo())
  75. // console.log(getThree())
  76.  
  77. getThree(true)
  78.   .then(data => {
  79.     console.log(data)
  80.     return getFour(data + 1)
  81.     // .then(fourData => console.log(fourData))
  82.     // .catch(err => console.log(err))
  83.     // console.log(getFour(data + 1))
  84.   })
  85.   .then(data => console.log(data))
  86.   .catch(err => console.log(err))
  87.  
  88. // console.log(getFour(4))
  89.  
  90. //callback hell(nesting callback)
  91.  
  92. //Async await uses promise (Behind the scene)
  93.  
  94. //fetch
  95.  
  96. // fetch('https://jsonplaceholder.typicode.com/todos')
  97. //   .then(res => res.json())
  98. //   .then(data => console.log(data))
  99. //   .catch(err => console.log(err))
  100.  
  101. async function init() {
  102.   try {
  103.     const todos = await (
  104.       await fetch('https://jsonplaceholder.typicode.com/todos')
  105.     ).json()
  106.     // const todos = await res.json()
  107.     return todos
  108.   } catch (err) {
  109.     console.log(err)
  110.   }
  111. }
  112.  
  113. ;(async function () {
  114.   console.log(await init())
  115. })()
  116.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement