Yield! Javascript Generators and Monocle.js

Yield! Javascript Generators and Monocle.js

In this talk I explain generators, a new Javascript feature coming in ES6. I then discuss the Monocle.js library, which uses generators to make writing asynchronous code much cleaner.

174ae1c2a863b7daf240a86da84671bd?s=128

Jonathan Lipps

August 29, 2013
Tweet

Transcript

  1. Jonathan Lipps Senior Developer Yield! Javascript Generators and Monocle.js Utah

    JS • 8/29/2013
  2. In ES6, a bold new world awaits, ripe for the

    plunder of its sweet fruits!* * This is meant to be humorous. Remember kids: imperialism is bad.
  3. My favorite is the generator, a new type of function

    whose execution can be externally controlled \
  4. Generators are available in node 11 and up, by passing

    the --harmony ag to node \
  5. Let’s have a look, shall we? \

  6. None
  7. Things to remember Calling a Generator returns an Iterator Iterators

    have a next() method Calling next() returns whatever is after the next yield statement in the generator
  8. Generators allow lazy construction of potentially in nite sequences \

  9. None
  10. Code in generators can respond to behavior in the outside

    world! \
  11. None
  12. None
  13. Things to remember next() actually returns an object, with keys

    value and done Calling next() with a parameter sends that value in as the result of the generator’s last yield Calling next() always returns whatever is after the next yield statement in the generator, even if you are also sending in a value
  14. Hang on a minute! If we can arbitrarily pause function

    execution, and send values back in...
  15. ...we can tell the generator to wait until we’ve received

    the result of an asynchronous function’s callback...
  16. ...and avoid callback hell! We all love to hate callback

    hell.
  17. None
  18. Rightward drift

  19. Repetitive error handling

  20. monocle.js is a library that lets you use a blocking-like

    syntax with async functions in generators
  21. https://github.com/jlipps/monocle-js

  22. monocle.js is a port of a Python project with the

    same basic goal
  23. https://github.com/saucelabs/monocle

  24. So long, callback hell!

  25. None
  26. o_O takes a generator and converts it into what we

    call an ‘o-routine’
  27. Inside an o-routine, you can: yield to another o-routine: pause

    execution until the other o-routine has completed return the result of this o-routine: pass a response to an o-routine waiting on us
  28. monocle.launch takes an o-routine and starts executing it. You always

    need to kick things off this way!
  29. 1. Launch the main o-routine

  30. 2. main waits for myLibraryFunction to respond with a result

  31. 3. we wait for result of request

  32. 4. we wait for writing data to le

  33. 5. we wait for contents of le

  34. 6. we return contents of le to whoever was waiting

    on us
  35. 7. main writes out the response and execution nishes

  36. But... yield? yield in generators usually acts like yield “as

    in crops”: it provides a result to next() yield in o-routines acts like yield “as in traffic”: it waits for something else to nish.
  37. It’s easy to convert existing asynchronous functions into o-routines

  38. None
  39. monocle.callback gives you a special function you pass to an

    async function as its callback
  40. You can then yield to this special callback, and monocle

    will wait for it as if it were an o-routine
  41. monocle.monoclize() is a helper function that makes it easy to

    convert async libraries to o-routines
  42. None
  43. We want to monoclize Node’s fs

  44. First we clone the fs object

  45. Then we make a list of methods

  46. Then we monoclize each one

  47. CASE STUDY Appium & Yiewd

  48. Appium is a mobile automation framework written in Node. It’s

    based on the WebDriver protocol.
  49. https://github.com/appium/appium

  50. WebDriver is an HTTP-based spec. Each automation command is sent

    over the wire.
  51. Wd is a Node.js client library that implements this protocol.

    Appium uses Wd to write all its tests.
  52. https://github.com/admc/wd

  53. Wd uses Node’s HTTP libraries, which are async functions. Appium

    test code thus looks like this:
  54. None
  55. Yiewd is a monoclized version of Wd that converts all

    automation functions into o-routines
  56. https://github.com/jlipps/yiewd

  57. Thanks to monocle, 27 lines of Wd code turns into

    16 lines of Yiewd
  58. None
  59. In sum, monocle.js uses JS generators to open up a

    whole new world of code quality
  60. Join the ght! https://github.com/jlipps/monocle-js

  61. Questions? @jlipps @saucelabs http://saucelabs.com/careers

  62. Thanks! @jlipps @saucelabs http://saucelabs.com/careers

  63. We’re Hiring! @jlipps @saucelabs http://saucelabs.com/careers