{"action":"created","issue":{"url":"https://api.github.com/repos/koajs/koa/issues/718","repository_url":"https://api.github.com/repos/koajs/koa","labels_url":"https://api.github.com/repos/koajs/koa/issues/718/labels{/name}","comments_url":"https://api.github.com/repos/koajs/koa/issues/718/comments","events_url":"https://api.github.com/repos/koajs/koa/issues/718/events","html_url":"https://github.com/koajs/koa/pull/718","id":150861195,"number":718,"title":"reference app.wrappers[] for each mw","user":{"login":"fl0w","id":71882,"avatar_url":"https://avatars.githubusercontent.com/u/71882?v=3","gravatar_id":"","url":"https://api.github.com/users/fl0w","html_url":"https://github.com/fl0w","followers_url":"https://api.github.com/users/fl0w/followers","following_url":"https://api.github.com/users/fl0w/following{/other_user}","gists_url":"https://api.github.com/users/fl0w/gists{/gist_id}","starred_url":"https://api.github.com/users/fl0w/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fl0w/subscriptions","organizations_url":"https://api.github.com/users/fl0w/orgs","repos_url":"https://api.github.com/users/fl0w/repos","events_url":"https://api.github.com/users/fl0w/events{/privacy}","received_events_url":"https://api.github.com/users/fl0w/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2016-04-25T13:34:27Z","updated_at":"2016-04-25T21:05:48Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/koajs/koa/pulls/718","html_url":"https://github.com/koajs/koa/pull/718","diff_url":"https://github.com/koajs/koa/pull/718.diff","patch_url":"https://github.com/koajs/koa/pull/718.patch"},"body":"This is for https://github.com/koajs/compose/pull/55"},"comment":{"url":"https://api.github.com/repos/koajs/koa/issues/comments/214521637","html_url":"https://github.com/koajs/koa/pull/718#issuecomment-214521637","issue_url":"https://api.github.com/repos/koajs/koa/issues/718","id":214521637,"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-04-25T21:05:48Z","updated_at":"2016-04-25T21:05:48Z","body":"@juliangruber Sure thing! Basically a wrapper replaces any middleware with the middleware called on the function (`wrappers.forEach(wrapper => mw = wrapper(mw))`). Here's how a wrapper works (this code is for Koa v2 + a wrappers patch):\r\n\r\n```js\r\napp.wrappers = [co.wrap];\r\n\r\napp.use(function * (ctx, next) {\r\n  ctx.body = 'example';\r\n  yield next();\r\n});\r\n```\r\n\r\nThis is also useful for profilers, for instance you can do this:\r\n\r\n```js\r\napp.wrappers = [function (mw) {\r\n  return function () {\r\n    let start = process.hrtime();\r\n    return Promise.resolve().then(() => mw).then(function (val) {\r\n      let time = process.hrtime(start);\r\n      console.log(mw._name || mw.name + ': ' + time.join('.') + ' seconds';\r\n      return val;\r\n    });\r\n  };\r\n}];\r\n```"}}