Your SlideShare is downloading. ×
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Promises generatorscallbacks
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Promises generatorscallbacks

321

Published on

Many developers new to Node.js struggle with writing asynchronous code in a clean, concise, easy to maintain manner. There are many strategies for managing and preventing 'callback hell' in Node.js. …

Many developers new to Node.js struggle with writing asynchronous code in a clean, concise, easy to maintain manner. There are many strategies for managing and preventing 'callback hell' in Node.js. We'll walk through many of those strategies, from Promises to Generators to flow control libraries, and show that with a bit of forethought, writing asynchronous code in Javascript and Node.js can be easy and maintainable.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
321
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Promises, Generators & Callbacks! Oh my!
  • 2. Writing asynchronous code is hard
  • 3. asynchronous code is difficult to read
  • 4. asynchronous code is difficult to write
  • 5. asynchronous code is difficult to maintain
  • 6. Mike Frey
  • 7. Why is writing asynchronous code hard?
  • 8. Composition
  • 9. Patterns
  • 10. Callbacks
  • 11. Promises
  • 12. Generators
  • 13. Now entering Nerd War territory
  • 14. Goals
  • 15. Callbacks vs Promises vs Generators
  • 16. Callbacks vs Promises vs Generators x x
  • 17. No Winner
  • 18. Unbiased
  • 19. Informative
  • 20. Callbacks
  • 21. How do they work?
  • 22. Continuation Passing
  • 23. Pass a function to another function
  • 24. Ask for work now Handle result later
  • 25. askForWork(function(err, res) { // handle result later })
  • 26. Where are they used?
  • 27. Everywhere
  • 28. Node.js core
  • 29. User-land
  • 30. Even Promises
  • 31. Benefits
  • 32. Simple. Easy to use. Prolific. Fast.
  • 33. Problems
  • 34. Error Handling
  • 35. try{} catch(){}
  • 36. try{} catch(){}
  • 37. try { doWork(function(res) { // handle result }) } catch (err) { // handle error }
  • 38. ! doWork(function(err, res) { // handle error // handle result }) ! !
  • 39. Homework! read this: ! joyent.com/developers/ node/design/errors
  • 40. Z͈A͡ ̼͔̭͖͕̲ L͝ ͙͖̱ G̠͍̤̠͕ O͢ ̬̫ Z͗̒͊̅ͫ̎ ̩̲̤͙̟ Ả͒͐̚ ̥̞̥͜ L̀͊ͬ͡ ̮̲Ğͥ̈ͩ̓͒ ͕̘͉ O͍̼̘͇͔̠͐ !̓̾̆ͪ͆̚͞
  • 41. asynchronous or synchronous never both
  • 42. Fix your API: process.nextTick() setImmediate()
  • 43. Fix their API: dezalgo
  • 44. Callback Hell
  • 45. Composition problem
  • 46. Now Later
  • 47. Now Later Later-er
  • 48. Now Later Later-er Later-er-er
  • 49. Now Later Later-er Later-er-er Later-er-er-er
  • 50. There’s nothing forcing you to write ten levels of nested callbacks, but the pattern does make it easy for you to do so. - Raymond Julin (paraphrased)
  • 51. Now Later Later-er Later-er-er Later-er-er-er
  • 52. Now Later Later-er Later-er-er Later-er-er-er
  • 53. getA(function() { getB(function() { getC(function() { // do something }) }) })
  • 54. function handleA() { getB(handleB) } function handleB() { getC(handleC) } function handleC() { // do something } getA(handleA)
  • 55. async module
  • 56. async.waterfall([ getA, getB, getC ], function(err, result) { // do something })
  • 57. Callbacks ! Simple. Everywhere. Be careful.
  • 58. Promises
  • 59. How do they work?
  • 60. Eventual Result
  • 61. .then()
  • 62. var promise = doSomething() promise.then( function (result) { // success callback }, function (error) { // error callback })
  • 63. Now Later success Later failure
  • 64. Where are they used?
  • 65. jQuery AngularJS Ember User-land
  • 66. Chrome 32 Firefox 29 Opera 19 Node.js 0.11.13
  • 67. Benefits
  • 68. Composition: Chaining & Error handling
  • 69. .then() .then() .then()
  • 70. Now Later success Later-er success Later-er-er success Later-er-er-er success
  • 71. function addOne(num) { return new Promise( function(resolve, reject) { resolve(num+1) }) } ! addOne(0) .then(addOne) .then(addOne) .then(addOne) .then(console.log)
  • 72. function addOne(num) { return num+1 } ! var p = new Promise( function(res, rej) { res(0) }) .then(addOne) .then(addOne) .then(addOne) .then(console.log)
  • 73. Rejections bubble
  • 74. Errors bubble
  • 75. Now Later success Later-er success Later-er-er success Any failure
  • 76. getSpeakers('MidwestJS') .then(getGithubUsers) .then(getPublicRepos) .then(listRepos, handleError)
  • 77. Problems
  • 78. Slow
  • 79. Slow
  • 80. Incompatible Proposals & Implementations
  • 81. jQuery vs everyone else
  • 82. Promises ! Composable. Eventual Result.
  • 83. Generators
  • 84. What are they?
  • 85. How do they work?
  • 86. function*
  • 87. function* tick() { ! ! } ! ! ! !
  • 88. function* tick() { ! ! } ! var itr = tick() ! !
  • 89. function* tick() { ! ! } ! var itr = tick() itr.next() !
  • 90. function* tick() { yield 42 ! } ! var itr = tick() itr.next().value // 42 !
  • 91. yield
  • 92. function* tick() { yield 42 yield 43 } ! var itr = tick() itr.next().value // 42 itr.next().value // 43 itr.next().done // true
  • 93. function* tick() { var x = yield var y = yield } ! var itr = tick() itr.next() itr.next(42) // x becomes 42 itr.next(43) // y becomes 43
  • 94. function* tick() { var num = 0 while (!(yield num++)); } ! var itr = tick() itr.next().value // 0 itr.next().value // 1 itr.next().value // 2
  • 95. function* tick() { var num = 0 while (!(yield num++)); } ! var itr = tick() itr.next().value // 0 itr.next().value // 1 itr.next(true).done // true
  • 96. Replacing callbacks
  • 97. function delay(time, callback) { setTimeout(function() { callback('Slept for ' + time) }, time) }
  • 98. function delayThings() { delay(1000, function(result1) { console.log(result1) delay(1200, function(result2) { console.log(result2) }) }) } // Slept for 1000 // Slept for 1200
  • 99. function* delayThings() { var results1 = yield delay(1000) console.log(results1) ! var results2 = yield delay(1200) console.log(results2) }
  • 100. function run(generator) { function resume(value) { itr.next(value) } var itr = generator(resume) itr.next() }
  • 101. function* delayThings() { var results1 = yield delay(1000) console.log(results1) ! var results2 = yield delay(1200) console.log(results2) } !
  • 102. function* delayThings(resume) { var results1 = yield delay(1000, resume) console.log(results1) ! var results2 = yield delay(1200, resume) console.log(results2) } ! run(delayThings)
  • 103. More callbacks?
  • 104. thunkify
  • 105. // simplified from // https://github.com/visionmedia/node-thunkify ! function thunkify(fn){ return function(){ var args = Array.prototype.slice.call(arguments) return function(done){ args.push(function(){ done.apply(null, arguments) }) fn.apply(null, args) } } }
  • 106. delay = thunkify(delay) ! var thunk = delay(1000) ! thunk(function(result) { console.log(result) })
  • 107. function run(generator) { function resume(value) { itr.next(value) } var itr = generator(resume) itr.next() } !
  • 108. function run(generator) { function resume(ret) { var obj = itr.next(ret) if (obj.done) return obj.value(resume) } var itr = generator() resume() }
  • 109. function* delayThings() { var results1 = yield delay(1000) console.log(results1) ! var results2 = yield delay(1200) console.log(results2) } ! run(delayThings)
  • 110. yield Now Later
  • 111. yield Now yield Later Later-er
  • 112. yield Now yield Later yield Later-er Later-er-er
  • 113. yield Now yield Later yield Later-er yield Later-er-er Later-er-er-er …
  • 114. co
  • 115. var delayThings = co(function*() { var results1 = yield delay(1000) console.log(results1) ! var results2 = yield delay(1200) console.log(results2) }) ! delayThings()
  • 116. var delayThings = co(function*() { var delays = [delay(1000), delay(1200)] var results = yield delays ! console.log(results[0]) console.log(results[1]) }) ! delayThings()
  • 117. Where are they used?
  • 118. co koa User-land
  • 119. Firefox 31 Chrome (flag) Node.js v0.11.10 (flag) Opera (flag)
  • 120. Benefits
  • 121. Feels synchronous
  • 122. try{} catch(){}
  • 123. New cool
  • 124. Problems
  • 125. Support
  • 126. regenerator facebook.github.io/regenerator/
  • 127. traceur github.com/google/traceur-compiler
  • 128. Immature implementations
  • 129. Generators ! yield statement. Pausable function*. Built for iterators.
  • 130. Writing asynchronous code is hard
  • 131. But it doesn’t have to be!
  • 132. Call to action
  • 133. Explore
  • 134. Streams http://nodestreams.com http://highlandjs.org
  • 135. Thank you!
  • 136. Questions? ! References available here: github.com/mikefrey/cpg-talk

×