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 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. # co
  2. [![Gitter][gitter-image]][gitter-url]
  3. [![NPM version][npm-image]][npm-url]
  4. [![Build status][travis-image]][travis-url]
  5. [![Test coverage][coveralls-image]][coveralls-url]
  6. [![Downloads][downloads-image]][downloads-url]
  7. Generator based control flow goodness for nodejs and the browser,
  8. using promises, letting you write non-blocking code in a nice-ish way.
  9. ## Co v4
  10. `co@4.0.0` has been released, which now relies on promises.
  11. It is a stepping stone towards [ES7 async/await](https://github.com/lukehoban/ecmascript-asyncawait).
  12. The primary API change is how `co()` is invoked.
  13. Before, `co` returned a "thunk", which you then called with a callback and optional arguments.
  14. Now, `co()` returns a promise.
  15. ```js
  16. co(function* () {
  17. var result = yield Promise.resolve(true);
  18. return result;
  19. }).then(function (value) {
  20. console.log(value);
  21. }, function (err) {
  22. console.error(err.stack);
  23. });
  24. ```
  25. If you want to convert a `co`-generator-function into a regular function that returns a promise,
  26. you now use `co.wrap(fn*)`.
  27. ```js
  28. var fn = co.wrap(function* (val) {
  29. return yield Promise.resolve(val);
  30. });
  31. fn(true).then(function (val) {
  32. });
  33. ```
  34. ## Platform Compatibility
  35. `co@4+` requires a `Promise` implementation.
  36. For versions of node `< 0.11` and for many older browsers,
  37. you should/must include your own `Promise` polyfill.
  38. When using node 0.11.x or greater, you must use the `--harmony-generators`
  39. flag or just `--harmony` to get access to generators.
  40. When using node 0.10.x and lower or browsers without generator support,
  41. you must use [gnode](https://github.com/TooTallNate/gnode) and/or [regenerator](http://facebook.github.io/regenerator/).
  42. io.js is supported out of the box, you can use `co` without flags or polyfills.
  43. ## Installation
  44. ```
  45. $ npm install co
  46. ```
  47. ## Associated libraries
  48. Any library that returns promises work well with `co`.
  49. - [mz](https://github.com/normalize/mz) - wrap all of node's code libraries as promises.
  50. View the [wiki](https://github.com/visionmedia/co/wiki) for more libraries.
  51. ## Examples
  52. ```js
  53. var co = require('co');
  54. co(function *(){
  55. // yield any promise
  56. var result = yield Promise.resolve(true);
  57. }).catch(onerror);
  58. co(function *(){
  59. // resolve multiple promises in parallel
  60. var a = Promise.resolve(1);
  61. var b = Promise.resolve(2);
  62. var c = Promise.resolve(3);
  63. var res = yield [a, b, c];
  64. console.log(res);
  65. // => [1, 2, 3]
  66. }).catch(onerror);
  67. // errors can be try/catched
  68. co(function *(){
  69. try {
  70. yield Promise.reject(new Error('boom'));
  71. } catch (err) {
  72. console.error(err.message); // "boom"
  73. }
  74. }).catch(onerror);
  75. function onerror(err) {
  76. // log any uncaught errors
  77. // co will not throw any errors you do not handle!!!
  78. // HANDLE ALL YOUR ERRORS!!!
  79. console.error(err.stack);
  80. }
  81. ```
  82. ## Yieldables
  83. The `yieldable` objects currently supported are:
  84. - promises
  85. - thunks (functions)
  86. - array (parallel execution)
  87. - objects (parallel execution)
  88. - generators (delegation)
  89. - generator functions (delegation)
  90. Nested `yieldable` objects are supported, meaning you can nest
  91. promises within objects within arrays, and so on!
  92. ### Promises
  93. [Read more on promises!](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)
  94. ### Thunks
  95. Thunks are functions that only have a single argument, a callback.
  96. Thunk support only remains for backwards compatibility and may
  97. be removed in future versions of `co`.
  98. ### Arrays
  99. `yield`ing an array will resolve all the `yieldables` in parallel.
  100. ```js
  101. co(function* () {
  102. var res = yield [
  103. Promise.resolve(1),
  104. Promise.resolve(2),
  105. Promise.resolve(3),
  106. ];
  107. console.log(res); // => [1, 2, 3]
  108. }).catch(onerror);
  109. ```
  110. ### Objects
  111. Just like arrays, objects resolve all `yieldable`s in parallel.
  112. ```js
  113. co(function* () {
  114. var res = yield {
  115. 1: Promise.resolve(1),
  116. 2: Promise.resolve(2),
  117. };
  118. console.log(res); // => { 1: 1, 2: 2 }
  119. }).catch(onerror);
  120. ```
  121. ### Generators and Generator Functions
  122. Any generator or generator function you can pass into `co`
  123. can be yielded as well. This should generally be avoided
  124. as we should be moving towards spec-compliant `Promise`s instead.
  125. ## API
  126. ### co(fn*).then( val => )
  127. Returns a promise that resolves a generator, generator function,
  128. or any function that returns a generator.
  129. ```js
  130. co(function* () {
  131. return yield Promise.resolve(true);
  132. }).then(function (val) {
  133. console.log(val);
  134. }, function (err) {
  135. console.error(err.stack);
  136. });
  137. ```
  138. ### var fn = co.wrap(fn*)
  139. Convert a generator into a regular function that returns a `Promise`.
  140. ```js
  141. var fn = co.wrap(function* (val) {
  142. return yield Promise.resolve(val);
  143. });
  144. fn(true).then(function (val) {
  145. });
  146. ```
  147. ## License
  148. MIT
  149. [npm-image]: https://img.shields.io/npm/v/co.svg?style=flat-square
  150. [npm-url]: https://npmjs.org/package/co
  151. [travis-image]: https://img.shields.io/travis/tj/co.svg?style=flat-square
  152. [travis-url]: https://travis-ci.org/tj/co
  153. [coveralls-image]: https://img.shields.io/coveralls/tj/co.svg?style=flat-square
  154. [coveralls-url]: https://coveralls.io/r/tj/co
  155. [downloads-image]: http://img.shields.io/npm/dm/co.svg?style=flat-square
  156. [downloads-url]: https://npmjs.org/package/co
  157. [gitter-image]: https://badges.gitter.im/Join%20Chat.svg
  158. [gitter-url]: https://gitter.im/tj/co?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge