{"id":13410754,"url":"https://github.com/tj/co","last_synced_at":"2025-05-12T05:30:10.385Z","repository":{"id":8819702,"uuid":"10518659","full_name":"tj/co","owner":"tj","description":"The ultimate generator based flow-control goodness for nodejs (supports thunks, promises, etc)","archived":false,"fork":false,"pushed_at":"2020-12-15T07:22:26.000Z","size":437,"stargazers_count":11882,"open_issues_count":43,"forks_count":786,"subscribers_count":263,"default_branch":"master","last_synced_at":"2025-05-08T17:59:06.957Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tj.png","metadata":{"files":{"readme":"Readme.md","changelog":"History.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-06-06T04:06:16.000Z","updated_at":"2025-05-08T08:15:15.000Z","dependencies_parsed_at":"2022-07-06T03:01:58.273Z","dependency_job_id":null,"html_url":"https://github.com/tj/co","commit_stats":null,"previous_names":["visionmedia/co"],"tags_count":36,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tj%2Fco","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tj%2Fco/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tj%2Fco/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tj%2Fco/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tj","download_url":"https://codeload.github.com/tj/co/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253124213,"owners_count":21857612,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-30T20:01:08.915Z","updated_at":"2025-05-08T17:59:20.518Z","avatar_url":"https://github.com/tj.png","language":"JavaScript","readme":"# co\n\n[![Gitter][gitter-image]][gitter-url]\n[![NPM version][npm-image]][npm-url]\n[![Build status][travis-image]][travis-url]\n[![Test coverage][coveralls-image]][coveralls-url]\n[![Downloads][downloads-image]][downloads-url]\n\n  Generator based control flow goodness for nodejs and the browser,\n  using promises, letting you write non-blocking code in a nice-ish way.\n\n## Co v4\n\n  `co@4.0.0` has been released, which now relies on promises.\n  It is a stepping stone towards the [async/await proposal](https://github.com/lukehoban/ecmascript-asyncawait).\n  The primary API change is how `co()` is invoked.\n  Before, `co` returned a \"thunk\", which you then called with a callback and optional arguments.\n  Now, `co()` returns a promise.\n\n```js\nco(function* () {\n  var result = yield Promise.resolve(true);\n  return result;\n}).then(function (value) {\n  console.log(value);\n}, function (err) {\n  console.error(err.stack);\n});\n```\n\n  If you want to convert a `co`-generator-function into a regular function that returns a promise,\n  you now use `co.wrap(fn*)`.\n\n```js\nvar fn = co.wrap(function* (val) {\n  return yield Promise.resolve(val);\n});\n\nfn(true).then(function (val) {\n\n});\n```\n\n## Platform Compatibility\n\n  `co@4+` requires a `Promise` implementation.\n  For versions of node `\u003c 0.11` and for many older browsers,\n  you should/must include your own `Promise` polyfill.\n\n  When using node 0.10.x and lower or browsers without generator support,\n  you must use [gnode](https://github.com/TooTallNate/gnode) and/or [regenerator](http://facebook.github.io/regenerator/).\n\n  When using node 0.11.x, you must use the `--harmony-generators`\n  flag or just `--harmony` to get access to generators.\n\n  Node v4+ is supported out of the box, you can use `co` without flags or polyfills.\n\n## Installation\n\n```\n$ npm install co\n```\n\n## Associated libraries\n\nAny library that returns promises work well with `co`.\n\n- [mz](https://github.com/normalize/mz) - wrap all of node's code libraries as promises.\n\nView the [wiki](https://github.com/visionmedia/co/wiki) for more libraries.\n\n## Examples\n\n```js\nvar co = require('co');\n\nco(function *(){\n  // yield any promise\n  var result = yield Promise.resolve(true);\n}).catch(onerror);\n\nco(function *(){\n  // resolve multiple promises in parallel\n  var a = Promise.resolve(1);\n  var b = Promise.resolve(2);\n  var c = Promise.resolve(3);\n  var res = yield [a, b, c];\n  console.log(res);\n  // =\u003e [1, 2, 3]\n}).catch(onerror);\n\n// errors can be try/catched\nco(function *(){\n  try {\n    yield Promise.reject(new Error('boom'));\n  } catch (err) {\n    console.error(err.message); // \"boom\"\n }\n}).catch(onerror);\n\nfunction onerror(err) {\n  // log any uncaught errors\n  // co will not throw any errors you do not handle!!!\n  // HANDLE ALL YOUR ERRORS!!!\n  console.error(err.stack);\n}\n```\n\n## Yieldables\n\n  The `yieldable` objects currently supported are:\n\n  - promises\n  - thunks (functions)\n  - array (parallel execution)\n  - objects (parallel execution)\n  - generators (delegation)\n  - generator functions (delegation)\n\nNested `yieldable` objects are supported, meaning you can nest\npromises within objects within arrays, and so on!\n\n### Promises\n\n[Read more on promises!](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)\n\n### Thunks\n\nThunks are functions that only have a single argument, a callback.\nThunk support only remains for backwards compatibility and may\nbe removed in future versions of `co`.\n\n### Arrays\n\n`yield`ing an array will resolve all the `yieldables` in parallel.\n\n```js\nco(function* () {\n  var res = yield [\n    Promise.resolve(1),\n    Promise.resolve(2),\n    Promise.resolve(3),\n  ];\n  console.log(res); // =\u003e [1, 2, 3]\n}).catch(onerror);\n```\n\n### Objects\n\nJust like arrays, objects resolve all `yieldable`s in parallel.\n\n```js\nco(function* () {\n  var res = yield {\n    1: Promise.resolve(1),\n    2: Promise.resolve(2),\n  };\n  console.log(res); // =\u003e { 1: 1, 2: 2 }\n}).catch(onerror);\n```\n\n### Generators and Generator Functions\n\nAny generator or generator function you can pass into `co`\ncan be yielded as well. This should generally be avoided\nas we should be moving towards spec-compliant `Promise`s instead.\n\n## API\n\n### co(fn*).then( val =\u003e )\n\nReturns a promise that resolves a generator, generator function,\nor any function that returns a generator.\n\n```js\nco(function* () {\n  return yield Promise.resolve(true);\n}).then(function (val) {\n  console.log(val);\n}, function (err) {\n  console.error(err.stack);\n});\n```\n\n### var fn = co.wrap(fn*)\n\nConvert a generator into a regular function that returns a `Promise`.\n\n```js\nvar fn = co.wrap(function* (val) {\n  return yield Promise.resolve(val);\n});\n\nfn(true).then(function (val) {\n\n});\n```\n\n## License\n\n  MIT\n\n[npm-image]: https://img.shields.io/npm/v/co.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/co\n[travis-image]: https://img.shields.io/travis/tj/co.svg?style=flat-square\n[travis-url]: https://travis-ci.org/tj/co\n[coveralls-image]: https://img.shields.io/coveralls/tj/co.svg?style=flat-square\n[coveralls-url]: https://coveralls.io/r/tj/co\n[downloads-image]: http://img.shields.io/npm/dm/co.svg?style=flat-square\n[downloads-url]: https://npmjs.org/package/co\n[gitter-image]: https://badges.gitter.im/Join%20Chat.svg\n[gitter-url]: https://gitter.im/tj/co?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge\u0026utm_content=badge\n","funding_links":[],"categories":["Packages","JavaScript","Repository","Control Flow","Links","others","Legacy","Uncategorized","Convenience Utilities","包","Node.js"],"sub_categories":["Control flow","React Components","[`level-co`][level-co]","Control flow Generators","Uncategorized","Others","常用NPM工具模块"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftj%2Fco","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftj%2Fco","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftj%2Fco/lists"}