Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. // PROGRAM SET UP
  2.  
  3. // Blocking code
  4. function sleep(miliseconds) {
  5. var start = new Date().getTime();
  6. var expire = start + miliseconds
  7. while(new Date().getTime() < expire) {
  8. }
  9. }
  10.  
  11. // Timeout function to simulate actual ASYNC function that is wrapped in promise
  12. function timeoutPromise() {
  13. return new Promise(function(resolve, reject) {
  14. setTimeout(function() {
  15. resolve()
  16. }, 0)
  17. })
  18. }
  19.  
  20. // Program execution
  21. console.log('Main context start')
  22. asyncOperation()
  23. .then(() => {
  24. console.log('asyncOperation end')
  25. })
  26. console.log('Main context end')
  27.  
  28. // Different scenarios for asyncOperation()
  29. // ============== CASE 1 ============== //
  30. async function asyncOperation() {
  31. timeoutPromise()
  32. sleep(5000)
  33. console.log('sleep completed')
  34. }
  35.  
  36. // RESULT:
  37. // 'Main context start'
  38. // AFTER 5 SECONDS
  39. // 'sleep completed'
  40. // 'asyncOperation end'
  41. // 'Main context end'
  42.  
  43. // Blocking code runs immediately after asyncOperation() is invoked, and blocks main context
  44.  
  45.  
  46. // ============== CASE 2 ============== //
  47. async function asyncOperation() {
  48. await timeoutPromise()
  49. sleep(5000)
  50. console.log('sleep completed')
  51. }
  52.  
  53. // RESULT:
  54. // 'Main context start'
  55. // 'Main context end'
  56. // AFTER 5 SECONDS
  57. // 'sleep completed'
  58. // 'asyncOperation end'
  59.  
  60. // Main context triggers asyncOperation(), does not wait, and proceeds to print 'main context end'
  61. // Blocking code runs AFTER timeoutPromise() is completed (and so does not affect main context)
  62. // Blocking code blocks asyncOperation(). asyncOperation() completes after sleeping 5 seconds
  63.  
  64. // Note:
  65. await timeoutPromise()
  66. sleep(5000)
  67. console.log('sleep completed')
  68.  
  69. // is identical to
  70. timeoutPromise().then(function() {
  71. sleep(5000)
  72. console.log('sleep completed')
  73. })
  74.  
  75.  
  76. // ============== CASE 3 ============== //
  77. async function asyncOperation() {
  78. timeoutPromise()
  79. process.nextTick(function() {
  80. sleep(5000)
  81. console.log('sleep completed')
  82. })
  83. }
  84.  
  85. // RESULT:
  86. // 'Main context start'
  87. // 'Main context end'
  88. // AFTER 5 SECONDS
  89. // 'sleep completed'
  90. // 'asyncOperation end'
  91.  
  92. // Similar to CASE 1, but blocking code execution is deferred to next tick, and does not block main context
  93.  
  94.  
  95. // ============== CASE 4 ============== //
  96. async function asyncOperation() {
  97. await timeoutPromise()
  98. process.nextTick(function() {
  99. sleep(5000)
  100. console.log('sleep completed')
  101. })
  102. }
  103.  
  104. // RESULT:
  105. // 'Main context start'
  106. // 'Main context end'
  107. // 'asyncOperation end'
  108. // AFTER 5 SECONDS
  109. // 'sleep completed'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement