{"action":"created","issue":{"url":"https://api.github.com/repos/strongloop/express/issues/2809","labels_url":"https://api.github.com/repos/strongloop/express/issues/2809/labels{/name}","comments_url":"https://api.github.com/repos/strongloop/express/issues/2809/comments","events_url":"https://api.github.com/repos/strongloop/express/issues/2809/events","html_url":"https://github.com/strongloop/express/pull/2809","id":117709187,"number":2809,"title":"router: add async route handler support","user":{"login":"defunctzombie","id":84792,"avatar_url":"https://avatars.githubusercontent.com/u/84792?v=3","gravatar_id":"","url":"https://api.github.com/users/defunctzombie","html_url":"https://github.com/defunctzombie","followers_url":"https://api.github.com/users/defunctzombie/followers","following_url":"https://api.github.com/users/defunctzombie/following{/other_user}","gists_url":"https://api.github.com/users/defunctzombie/gists{/gist_id}","starred_url":"https://api.github.com/users/defunctzombie/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/defunctzombie/subscriptions","organizations_url":"https://api.github.com/users/defunctzombie/orgs","repos_url":"https://api.github.com/users/defunctzombie/repos","events_url":"https://api.github.com/users/defunctzombie/events{/privacy}","received_events_url":"https://api.github.com/users/defunctzombie/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2015-11-18T23:52:50Z","updated_at":"2015-11-19T00:14:10Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/strongloop/express/pulls/2809","html_url":"https://github.com/strongloop/express/pull/2809","diff_url":"https://github.com/strongloop/express/pull/2809.diff","patch_url":"https://github.com/strongloop/express/pull/2809.patch"},"body":"While the async/await feature is still highly experimental and not\r\nofficial, it does have preliminary support in tools like babel. There\r\nare even blog posts [1] about how to use the async/await feature with\r\nexpress routes.\r\n\r\nHowever, these posts along with issue #2789 and #2788 show that using\r\nasync/await is still a bit of a manual process and not clear. This\r\nchange inspects the return value of a route handler to see if it is\r\npotentially a pomise. If the return value appears to be a promise then\r\nwe attach to the error handler of the promise via .catch()\r\n\r\nNow a user who is using babel can write the following without needing\r\nany wrap functions as most examples to date require.\r\n\r\n```js\r\napp.get('/', async (req, res, next) => {\r\n    let user = await User.findById(); // assuming .findById() returns a primise\r\n    let org = await Org.findById();\r\n    res.json({\r\n        user: user,\r\n        org: org,\r\n    });\r\n});\r\n```\r\n\r\n[1] https://strongloop.com/strongblog/async-error-handling-expressjs-es7-promises-generators/\r\n\r\n--------------\r\n\r\nNote on testing:\r\nI have not added any tests since it would require instrumenting with babel but I can do that if the current maintainers think it would be useful. Alternatively, a fake test that just passes a promise like object to a route could simulate the same results.\r\n\r\nAlternative implementation:\r\nIf the conditional checking is not desired in the hot path, then a wrap function could be applied upon adding the actual route versus at runtime. Additionally, that would open up the possibility of putting this behind an express setting which I didn't want to do in the hot path but maybe that would also be ok."},"comment":{"url":"https://api.github.com/repos/strongloop/express/issues/comments/157905916","html_url":"https://github.com/strongloop/express/pull/2809#issuecomment-157905916","issue_url":"https://api.github.com/repos/strongloop/express/issues/2809","id":157905916,"user":{"login":"defunctzombie","id":84792,"avatar_url":"https://avatars.githubusercontent.com/u/84792?v=3","gravatar_id":"","url":"https://api.github.com/users/defunctzombie","html_url":"https://github.com/defunctzombie","followers_url":"https://api.github.com/users/defunctzombie/followers","following_url":"https://api.github.com/users/defunctzombie/following{/other_user}","gists_url":"https://api.github.com/users/defunctzombie/gists{/gist_id}","starred_url":"https://api.github.com/users/defunctzombie/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/defunctzombie/subscriptions","organizations_url":"https://api.github.com/users/defunctzombie/orgs","repos_url":"https://api.github.com/users/defunctzombie/repos","events_url":"https://api.github.com/users/defunctzombie/events{/privacy}","received_events_url":"https://api.github.com/users/defunctzombie/received_events","type":"User","site_admin":false},"created_at":"2015-11-19T00:14:10Z","updated_at":"2015-11-19T00:14:10Z","body":"If v4 is frozen then I am certainly up to do the change against the router repo if that is what will be used going forward. Just surfaced this based on my current usage (which is all v4)."}}