Syncing Async

Syncing Async

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.

35761e3936deba2f8189c2d20982c771?s=128

Kyle Simpson

October 16, 2015
Tweet

Transcript

  1. syncing async kyle simpson @getify http://getify.me

  2. vs. parallel async

  3. threads

  4. None
  5. None
  6. None
  7. None
  8. event-loop concurrency

  9. 1 2 3 4 1 2 3

  10. 1 2 3 4 1 2 3 done! done!

  11. async patterns

  12. callbacks == continuations

  13. callbacks got problems 2 major ^

  14. “callback hell”

  15. still “callback hell”

  16. inversion of control

  17. None
  18. 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 ...
  19. None
  20. nested callbacks are not reasonable

  21. None
  22. None
  23. we write

  24. we think

  25. js thinks

  26. sync-looking async synchronous sequential blocking

  27. hell is callbacks -Sartre

  28. promise: future value

  29. promise: “completion” event

  30. None
  31. None
  32. promises: 1. only resolved once 2. either success OR error

    3. messages passed/kept 4. exceptions become errors 5. immutable once resolved
  33. uninversion of control

  34. steps

  35. promise: flow control

  36. None
  37. scenario: load files in parallel, render in order, but ASAP

  38. None
  39. cool story, bro. but...

  40. None
  41. scenario: timeout of a promise

  42. None
  43. blog.getify.com/promises-part-1/ github.com/getify/native-promise-only

  44. promise abstractions

  45. sequence = series of automatically chained promises

  46. github.com/getify/asynquence async + sequence = asynquence

  47. None
  48. remember? load files in parallel, render in order, but ASAP

  49. None
  50. None
  51. promises without all the fuss

  52. gimme moar sync

  53. ES6 generators davidwalsh.name/es6-generators

  54. generator + promises yield promises

  55. ES2016 async ? github.com/lukehoban/ecmascript-asyncawait

  56. None
  57. None
  58. github.com/getify/a-tale-of-three-lists

  59. “if you like it then you shoulda put async in

    it...”
  60. thx! kyle simpson @getify http://getify.me