Syncing Async

of 70/70
syncing async kyle simpson @getify http://getify.me
  • date post

    29-Nov-2014
  • Category

    Internet

  • view

    1.797
  • download

    5

Embed Size (px)

description

Syncing Async with Kyle Simpson Presented on September 17 2014 at FITC's Web Unleashed Toronto 2014 I bet you think “callback hell” is about function nesting and levels of indentation. Not so much. I bet you’ve heard that Promises replace callbacks. Nope. We need some clarity on what async flow control is all about in JavaScript. We’ll explore the perils of “Inversion of Control”, and see just what hell that leads us to. Then we’ll talk about promises as “continuation events” for function calls, and abstractions on top of promises that clean up our code. Finally, we’ll see how generators give us synchronous-looking async, and even how we can create cooperative concurrency with coroutines. Yield those confused async notions and control your flow. I promise we’ll get your thoughts in order. OBJECTIVE Learn why async flow control patterns are necessary for JS, why callbacks aren’t sufficient and how promises and generators are the solution. TARGET AUDIENCE JS developers ASSUMED AUDIENCE KNOWLEDGE Intermediate to advanced JS (3+ years experience). FIVE THINGS AUDIENCE MEMBERS WILL LEARN Why our brains reason about code differently than the JS engine does Why callback hell has much less to do with nesting/indentation than you thought What inversion of control is all about, and what trust issues it creates for our code What promises are and why they not only make code easier to reason about, but also solve inversion of control issues Why generators are even better, and how generators+promises are potentially the best pattern we can achieve thus far

Transcript of Syncing Async

  • 1. syncing async kyle simpson @getify http://getify.me
  • 2. to get started...
  • 3. parallel vs. async
  • 4. threads
  • 5. event-loop concurrency
  • 6. 123 1234
  • 7. 1 1 2 23 34 done! done!
  • 8. async patterns
  • 9. callbacks == continuations
  • 10. callbacks got problems 2 major ^
  • 11. callback hell
  • 12. still callback hell
  • 13. inversion of control
  • 14. trust: 1. not too early 2. not too late 3. not too many times 4. not too few times 5. no lost context 6. no swallowed errors ...
  • 15. nested callbacks are not reasonable
  • 16. we write
  • 17. many steps such sync very wow
  • 18. we think
  • 19. js thinks
  • 20. sync-looking async synchronous sequential blocking
  • 21. hell is callbacks -Sartre
  • 22. promise: future value
  • 23. promise: completion event
  • 24. promises: 1. only resolved once 2. either success OR error 3. messages passed/kept 4. exceptions become errors 5. immutable once resolved
  • 25. uninversion of control
  • 26. steps
  • 27. promise: flow control
  • 28. scenario: load files in parallel, render in order, but ASAP
  • 29. cool story, bro. but...
  • 30. scenario: timeout of a promise
  • 31. blog.getify.com/promises-part-1/ github.com/getify/native-promise-only
  • 32. promise abstractions
  • 33. sequence = series of automatically chained promises
  • 34. async + sequence = asynquence github.com/getify/asynquence
  • 35. remember? load files in parallel, render in order, but ASAP
  • 36. promises without all the fuss
  • 37. gimme moar sync
  • 38. ES6 generators davidwalsh.name/es6-generators
  • 39. yield promises generator + promises
  • 40. what about pre-ES6 browsers?
  • 41. transpiled ES5 generators facebook.github.io/regenerator/
  • 42. ~ES7 async ? github.com/lukehoban/ecmascript-asyncawait
  • 43. CSP-like concurrency
  • 44. .done() chuckle, chuckle
  • 45. thx! kyle simpson @getify http://getify.me