Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Yield! How ES6 Generators and Monocle-js Can Br...

Yield! How ES6 Generators and Monocle-js Can Bring Async Into Line, Literally

"Callback hell" is an oft-bemoaned state of Javascript programs. Some developers have seen their code trapped in its Ninth Circle and have lost all hope of getting it out. Others are callback agnostics and don't believe this so-called hell even exists. And most of us are probably somewhere in between: working in that pyramid-shaped purgatory where, we think, if we do our Good Parts penitence, we will eventually get our code to the heaven of Readability and Maintainability.

While smart practices and async helpers can limit the pain of callback-based control flow, ES6 generators make possible new approaches that result in code which is much easier to read, maintain, and reason about. Monocle-js is one such project. In this talk, we'll look at some of the control flow situations which are particularly ugly in callback-based approaches, then discuss ES6's new generator support and `yield` keyword. With a basic understanding of generators in hand, we'll explore how Monocle-js can drag the aforementioned examples out of callback purgatory and set them on the straight and narrow path to Readability and Maintainability.

Jonathan Lipps

May 10, 2014
Tweet

More Decks by Jonathan Lipps

Other Decks in Programming

Transcript

  1. Yield! Or, How ES6 Generators and Monocle-js Can Bring Async

    Into Line, Literally ScotlandJS • Edinburgh • 5/10/2014 #sjsJonathanL Jonathan Lipps • Director of Ecosystem & Integrations • Sauce Labs @jlipps • @saucelabs
  2. #sjsJonathanL Jonathan Lipps • Director of Ecosystem & Integrations •

    Sauce Labs @jlipps • @saucelabs Ecosystem & Integrations
  3. Sync #sjsJonathanL @jlipps Async Blocking Immediate execution Non-blocking Result captured

    by assignment Errors handled with try/catch Deferred execution
  4. Sync #sjsJonathanL @jlipps Async Blocking Immediate execution Non-blocking Result captured

    by assignment Errors handled with try/catch Deferred execution Result captured in callback
  5. Sync #sjsJonathanL @jlipps Async Blocking Immediate execution Non-blocking Result captured

    by assignment Errors handled with try/catch Deferred execution Result captured in callback Errors handled in callback
  6. x = 1 y = x + 2 y <

    x #sjsJonathanL @jlipps
  7. x = 1 y = x + 2 y <

    x y++ y-- #sjsJonathanL @jlipps
  8. x = 1 y = x + 2 y <

    x y++ y-- x++ #sjsJonathanL @jlipps
  9. Async control ow can be tricky when there are dependencies

    between methods #sjsJonathanL @jlipps
  10. 1. Visual unreadability / “Pyramid effect” 2. Error handling 3.

    Control ow: conditionals #sjsJonathanL @jlipps
  11. 1. Visual unreadability / “Pyramid effect” 2. Error handling 3.

    Control ow: conditionals 4. Control ow: loops #sjsJonathanL @jlipps
  12. Async control ow with callbacks involves lots of boilerplate. We

    can haz framework? #sjsJonathanL @jlipps
  13. Callback-based control ow strategies can be at best a clean

    re-implementation of JS’s own control statements. Can we do better? #sjsJonathanL @jlipps
  14. Generators are a new ES6 feature available in node 11

    and up, by passing the --harmony ag to node #sjsJonathanL @jlipps
  15. Code in generators can also respond to behavior in the

    outside world! They do this with the new yield keyword. #sjsJonathanL @jlipps
  16. If we can “pause” a generator until someone outside calls

    next(), we can use yield to model waiting for an async function to call back #sjsJonathanL @jlipps
  17. In yield asyncFunction(), “asyncFunction” is not just a function: it

    is an “o-routine”. It can also be a Promise if you’re into that #sjsJonathanL @jlipps
  18. Yields in generators often mean “yield up”. When using them

    in Monocle, they mean “yield to” #sjsJonathanL @jlipps
  19. Error handling is the same as in regular sync JavaScript

    control ow: use try/catch #sjsJonathanL @jlipps
  20. Control ow is back in sync style! We can now

    use the built in control statements. #sjsJonathanL @jlipps
  21. Monocle-js lets you write async, non-blocking code using the built-in

    language features that are the most easy to reason about #sjsJonathanL @jlipps
  22. #sjsJonathanL @jlipps @nexxylove: “@jlipps is right about a lot of

    things, which means that it’s probably OK if he’s wrong about this one thing. I don’t want to generate any controversy here; I’m sure we can iterate on the idea and make it work for everyone!”
  23. There are lots of ways to write good JS code.

    Monocle-js has advantages and disadvantages, like every other approach #sjsJonathanL @jlipps
  24. If you write code with lots of serial async functions,

    Monocle-js is worth a look. #sjsJonathanL @jlipps https://github.com/jlipps/monocle-js
  25. Either way, educate yourself on good habits for async control

    ow in all approaches #sjsJonathanL @jlipps https://github.com/jlipps/async-showcase