You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. degenerator
  2. ===========
  3. ### Turns sync functions into async generator functions
  4. [![Build Status](https://travis-ci.org/TooTallNate/node-degenerator.svg?branch=master)](https://travis-ci.org/TooTallNate/node-degenerator)
  5. Sometimes you need to write sync looking code that's really async under the hood.
  6. This module takes a String to one or more synchronous JavaScript functions, and
  7. returns a new String that with those JS functions transpiled into ES6 Generator
  8. Functions.
  9. So this:
  10. ``` js
  11. function foo () {
  12. return a('bar') || b();
  13. }
  14. ```
  15. Gets compiled into:
  16. ``` js
  17. function* foo() {
  18. return (yield a('bar')) || (yield b());
  19. }
  20. ```
  21. From there, you can provide asynchronous thunk-based or Generator-based
  22. implementations for the `a()` and `b()` functions, in conjunction with any
  23. Generator-based flow control library to execute the contents of the
  24. function asynchronously.
  25. Installation
  26. ------------
  27. Install with `npm`:
  28. ``` bash
  29. $ npm install degenerator
  30. ```
  31. Example
  32. -------
  33. You must explicitly specify the names of the functions that should be
  34. "asyncified". So say we wanted to expose a `get(url)` function that did
  35. and HTTP request and returned the response body.
  36. The user has provided us with this implementation:
  37. ``` js
  38. function myFn () {
  39. var one = get('https://google.com');
  40. var two = get('http://nodejs.org');
  41. var three = JSON.parse(get('http://jsonip.org'));
  42. return [one, two, three];
  43. }
  44. ```
  45. Now we can compile this into an asyncronous generator function, implement the
  46. async `get()` function, and finally evaluate it into a real JavaScript function
  47. instance with the `vm` module:
  48. ``` js
  49. var co = require('co');
  50. var vm = require('vm');
  51. var degenerator = require('degenerator');
  52. // the `get()` function is thunk-based (error handling omitted for brevity)
  53. function get (endpoint) {
  54. return function (fn) {
  55. var mod = 0 == endpoint.indexOf('https:') ? require('https') : require('http');
  56. var req = mod.get(endpoint);
  57. req.on('response', function (res) {
  58. var data = '';
  59. res.setEncoding('utf8');
  60. res.on('data', function (b) { data += b; });
  61. res.on('end', function () {
  62. fn(null, data);
  63. });
  64. });
  65. };
  66. }
  67. // convert the JavaScript string provided from the user (assumed to be `str` var)
  68. str = degenerator(str, [ 'get' ]);
  69. // at this stage, you could use a transpiler like `facebook/regenerator`
  70. // here if desired.
  71. // turn the JS String into a real GeneratorFunction instance
  72. var genFn = vm.runInNewContext('(' + str + ')', { get: get });
  73. // use a generator-based flow control library (`visionmedia/co`, `jmar777/suspend`,
  74. // etc.) to create an async function from the generator function.
  75. var asnycFn = co(genFn);
  76. // NOW USE IT!!!
  77. asyncFn(function (err, res) {
  78. // ...
  79. });
  80. ```
  81. API
  82. ---
  83. ### degenerator(String jsStr, Array functionNames) → String
  84. Returns a "degeneratorified" JavaScript string, with ES6 Generator
  85. functions transplanted.
  86. License
  87. -------
  88. (The MIT License)
  89. Copyright (c) 2013 Nathan Rajlich <nathan@tootallnate.net>
  90. Permission is hereby granted, free of charge, to any person obtaining
  91. a copy of this software and associated documentation files (the
  92. 'Software'), to deal in the Software without restriction, including
  93. without limitation the rights to use, copy, modify, merge, publish,
  94. distribute, sublicense, and/or sell copies of the Software, and to
  95. permit persons to whom the Software is furnished to do so, subject to
  96. the following conditions:
  97. The above copyright notice and this permission notice shall be
  98. included in all copies or substantial portions of the Software.
  99. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
  100. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  101. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  102. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  103. CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  104. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  105. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.