{"action":"created","issue":{"url":"https://api.github.com/repos/koajs/koa/issues/533","repository_url":"https://api.github.com/repos/koajs/koa","labels_url":"https://api.github.com/repos/koajs/koa/issues/533/labels{/name}","comments_url":"https://api.github.com/repos/koajs/koa/issues/533/comments","events_url":"https://api.github.com/repos/koajs/koa/issues/533/events","html_url":"https://github.com/koajs/koa/issues/533","id":111732707,"number":533,"title":"Koa 2.0.0","user":{"login":"jonathanong","id":643505,"avatar_url":"https://avatars.githubusercontent.com/u/643505?v=3","gravatar_id":"","url":"https://api.github.com/users/jonathanong","html_url":"https://github.com/jonathanong","followers_url":"https://api.github.com/users/jonathanong/followers","following_url":"https://api.github.com/users/jonathanong/following{/other_user}","gists_url":"https://api.github.com/users/jonathanong/gists{/gist_id}","starred_url":"https://api.github.com/users/jonathanong/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jonathanong/subscriptions","organizations_url":"https://api.github.com/users/jonathanong/orgs","repos_url":"https://api.github.com/users/jonathanong/repos","events_url":"https://api.github.com/users/jonathanong/events{/privacy}","received_events_url":"https://api.github.com/users/jonathanong/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/koajs/koa/labels/documentation","name":"documentation","color":"006b75"}],"state":"open","locked":false,"assignee":null,"milestone":{"url":"https://api.github.com/repos/koajs/koa/milestones/1","html_url":"https://github.com/koajs/koa/milestones/v2.0.0","labels_url":"https://api.github.com/repos/koajs/koa/milestones/1/labels","id":856817,"number":1,"title":"v2.0.0","description":"bout time","creator":{"login":"tj","id":25254,"avatar_url":"https://avatars.githubusercontent.com/u/25254?v=3","gravatar_id":"","url":"https://api.github.com/users/tj","html_url":"https://github.com/tj","followers_url":"https://api.github.com/users/tj/followers","following_url":"https://api.github.com/users/tj/following{/other_user}","gists_url":"https://api.github.com/users/tj/gists{/gist_id}","starred_url":"https://api.github.com/users/tj/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tj/subscriptions","organizations_url":"https://api.github.com/users/tj/orgs","repos_url":"https://api.github.com/users/tj/repos","events_url":"https://api.github.com/users/tj/events{/privacy}","received_events_url":"https://api.github.com/users/tj/received_events","type":"User","site_admin":false},"open_issues":15,"closed_issues":11,"state":"open","created_at":"2014-11-07T02:18:36Z","updated_at":"2016-03-12T21:49:02Z","due_on":null,"closed_at":null},"comments":171,"created_at":"2015-10-16T00:10:01Z","updated_at":"2016-03-12T23:59:35Z","closed_at":null,"body":"# Roadmap\r\n\r\nWe do not plan to release v2 as \"stable\" until async/await hits node.js. Requiring babel to run your server is just not good developer experience. I do this right now, and I run into a host of problems. Writing your middleware with just promises or with `co()` or `Bluebird.coroutine()` everywhere isn't a good experience either. The current way to write middleware is ideal until async/await is native.\r\n\r\nYou can track v8's progress on async functions here: https://bugs.chromium.org/p/v8/issues/detail?id=4483. After v8 implements async functions, it will take some time for that v8 version to hit stable (6 weeks i think, not sure), then more time for that v8 version to hit node stable (i believe they plan to release a major version every 6 months). \r\n\r\nThus, once async functions hit v8, we have between 2-7 months to release Koa v2 as stable, but it'll probably just take us a day since not much other than the middeware signature has changed.\r\n\r\nDon't be scared to use Koa v2 Alpha right now, though. People are already using it in production, and we don't foresee any major changes until we mark it stable.\r\n\r\n# Changes\r\n\r\nThe new middleware signature is:\r\n\r\n```js\r\n// uses async arrow functions\r\napp.use(async (ctx, next) => {\r\n  try {\r\n    await next() // next is now a function\r\n  } catch (err) {\r\n    ctx.body = { message: err.message }\r\n    ctx.status = err.status || 500\r\n  }\r\n})\r\n\r\napp.use(async ctx => {\r\n  const user = await User.getById(this.session.userid) // await instead of yield\r\n  ctx.body = user // ctx instead of this\r\n})\r\n```\r\n\r\nYou don't have to use asynchronous functions - you just have to pass a function that returns a promise. A regular function that returns a promise works too!\r\n\r\nWe don't know how much performance difference there will be, nor do many of the maintainers really care. Koa is as minimal as a framework can be and will not be your app's bottleneck.\r\n\r\n# New Features\r\n\r\nHopefully, Koa v2 will not have any new features as new features can be added to v1 as well. The only new features that will be added to Koa v2 will be breaking changes. Some possible features are:\r\n\r\n- https://github.com/koajs/koa/issues/469\r\n- https://github.com/koajs/koa/issues/281\r\n\r\nFeatures for HTTP2 support can still go into Koa v1. The only problem is that if it's not in `require('http')` or `require('https')`, we're not going to include it in Koa. [node-spdy](https://github.com/indutny/node-spdy) is probably going to be the code that is merged into node.js.\r\n\r\n# Middleware\r\n\r\nAll of the current stable versions of middleware should be targeting koa v1. If it doesn't, let us know and we'll fix it.\r\n\r\nMiddleware may have an \"alpha\" version of the koa v2 version. These should NOT be marked as stable. If they do not exist, let us know and we'll create an alpha version so you can try it with koa v2. Better yet, make a PR!\r\n\r\n# Upgrading Middleware\r\n\r\nYou will have to convert your generators to async functions with the new middleware signature:\r\n\r\n```js\r\napp.use(async ctx => {\r\n  const user = await Users.getById(this.session.user_id)\r\n  ctx.body = { message: 'some message' }\r\n})\r\n```\r\n\r\nUpgrading your middleware may be a pain in the ass. One migration path is to update them one-by-one.\r\n\r\n1. Wrap all your current middleware in [koa-convert](https://github.com/gyson/koa-convert)\r\n2. Test \r\n3. `npm outdated` to see which koa middleware is outdated\r\n4. Update one outdated middleware, remove using [koa-convert](https://github.com/gyson/koa-convert)\r\n5. Test\r\n6. Repeat steps 3-5 until you're done\r\n\r\nWe have plans to create a migration bot #625 to make this process slightly better.\r\n\r\n# Updating Your Code\r\n\r\nYou should start refactoring your code now to ease migrating to Koa v2:\r\n\r\n- Return promises everywhere!\r\n- Do not use `yield*`\r\n- Do not use `yield {}` or `yield []`. \r\n  - Convert `yield []` into `yield Promise.all([])`\r\n  - Convert `yield {}` into `yield Bluebird.props({})`\r\n\r\nYou could also refactor your logic outside of Koa middleware functions. Create functions like `function* someLogic(ctx) {}` and call it in your middleware as `const result = yield someLogic(this)`. Not using `this` will help migrations to the new middleware signature, which does not use `this`.\r\n\r\nI currently use babel and use async functions + flow type outside of koa middleware. this will make my migration path in the future easier. I'm not sure if I plan to ever remove babel though since I really like flow type.\r\n\r\n# How You Can Help\r\n\r\n- Documentation - we need a lot!\r\n- Testing Koa v2 and its corresponding middleware\r\n- Help create the migration bot https://github.com/koajs/koa/issues/625\r\n\r\n# Questions\r\n\r\n- Should we transpile w/ babel before we publish? It might make performance better as it transpiles down to ES5, which V8 is more optimized with. Anyone wanna try benchmarking it?"},"comment":{"url":"https://api.github.com/repos/koajs/koa/issues/comments/195836868","html_url":"https://github.com/koajs/koa/issues/533#issuecomment-195836868","issue_url":"https://api.github.com/repos/koajs/koa/issues/533","id":195836868,"user":{"login":"PlasmaPower","id":6006498,"avatar_url":"https://avatars.githubusercontent.com/u/6006498?v=3","gravatar_id":"","url":"https://api.github.com/users/PlasmaPower","html_url":"https://github.com/PlasmaPower","followers_url":"https://api.github.com/users/PlasmaPower/followers","following_url":"https://api.github.com/users/PlasmaPower/following{/other_user}","gists_url":"https://api.github.com/users/PlasmaPower/gists{/gist_id}","starred_url":"https://api.github.com/users/PlasmaPower/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/PlasmaPower/subscriptions","organizations_url":"https://api.github.com/users/PlasmaPower/orgs","repos_url":"https://api.github.com/users/PlasmaPower/repos","events_url":"https://api.github.com/users/PlasmaPower/events{/privacy}","received_events_url":"https://api.github.com/users/PlasmaPower/received_events","type":"User","site_admin":false},"created_at":"2016-03-12T23:59:35Z","updated_at":"2016-03-12T23:59:35Z","body":"@ide A big problem is then the transition will often be generators -> promises -> async/await instead of just generators -> async/await."}}