{"action":"created","issue":{"url":"https://api.github.com/repos/strongloop/express/issues/2259","labels_url":"https://api.github.com/repos/strongloop/express/issues/2259/labels{/name}","comments_url":"https://api.github.com/repos/strongloop/express/issues/2259/comments","events_url":"https://api.github.com/repos/strongloop/express/issues/2259/events","html_url":"https://github.com/strongloop/express/issues/2259","id":38590049,"number":2259,"title":"promises","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/strongloop/express/labels/5.x","name":"5.x","color":"ededed"},{"url":"https://api.github.com/repos/strongloop/express/labels/discuss","name":"discuss","color":"02d7e1"},{"url":"https://api.github.com/repos/strongloop/express/labels/enhancement","name":"enhancement","color":"84b6eb"}],"state":"open","locked":false,"assignee":{"login":"dougwilson","id":67512,"avatar_url":"https://avatars.githubusercontent.com/u/67512?v=3","gravatar_id":"","url":"https://api.github.com/users/dougwilson","html_url":"https://github.com/dougwilson","followers_url":"https://api.github.com/users/dougwilson/followers","following_url":"https://api.github.com/users/dougwilson/following{/other_user}","gists_url":"https://api.github.com/users/dougwilson/gists{/gist_id}","starred_url":"https://api.github.com/users/dougwilson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dougwilson/subscriptions","organizations_url":"https://api.github.com/users/dougwilson/orgs","repos_url":"https://api.github.com/users/dougwilson/repos","events_url":"https://api.github.com/users/dougwilson/events{/privacy}","received_events_url":"https://api.github.com/users/dougwilson/received_events","type":"User","site_admin":false},"milestone":null,"comments":46,"created_at":"2014-07-24T01:27:09Z","updated_at":"2015-06-07T09:19:08Z","closed_at":null,"body":"now that promises are going mainstream, i'm trying to think of how to make express more async friendly. an idea is to use promises.\r\n\r\n- `next()` now returns a promise\r\n- if middleware returns a promise, that promise is resolved and propagated up `next()`s\r\n\r\n```js\r\napp.use(function (req, res, next) {\r\n  // a promise must be returned, \r\n  // otherwise the function will be assumed to be synchronous\r\n  return User.get(req.session.userid).then(function (user) {\r\n    req.user = user\r\n  })\r\n  .then(next) // execute all downstream middleware\r\n  .then(function () {\r\n    // send a response after all downstream middleware have executed\r\n    // this is equivalent to koa's \"upstream\"\r\n    res.send(user)\r\n  })\r\n})\r\n```\r\n\r\nError handlers are now more koa-like:\r\n\r\n```js\r\napp.use(function (req, res, next) {\r\n  return next().catch(function (err) {\r\n    // a custom error handler\r\n    if (res.headerSent) return\r\n    res.statusCode = err.status || 500\r\n    res.send(err.message) \r\n  })\r\n})\r\n\r\napp.use(function (req, res) {\r\n  throw new Error('hahahah')\r\n})\r\n```\r\n\r\nPros:\r\n\r\n- it __should__ be backwards compatible since you don't have to resolve the promise returned from `next()`\r\n- much easier error handling including `throw`ing\r\n- solves issues shown in https://github.com/visionmedia/express/issues/2255\r\n- no more `fn.length` checking ~_~\r\n- could probably easily upgrade to es7 async functions\r\n\r\nCons:\r\n\r\n- promises\r\n- upgrading middleware and supporting both signatures might be a pain in the ass\r\n- probably a lot slower"},"comment":{"url":"https://api.github.com/repos/strongloop/express/issues/comments/109725432","html_url":"https://github.com/strongloop/express/issues/2259#issuecomment-109725432","issue_url":"https://api.github.com/repos/strongloop/express/issues/2259","id":109725432,"user":{"login":"joepie91","id":1663259,"avatar_url":"https://avatars.githubusercontent.com/u/1663259?v=3","gravatar_id":"","url":"https://api.github.com/users/joepie91","html_url":"https://github.com/joepie91","followers_url":"https://api.github.com/users/joepie91/followers","following_url":"https://api.github.com/users/joepie91/following{/other_user}","gists_url":"https://api.github.com/users/joepie91/gists{/gist_id}","starred_url":"https://api.github.com/users/joepie91/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/joepie91/subscriptions","organizations_url":"https://api.github.com/users/joepie91/orgs","repos_url":"https://api.github.com/users/joepie91/repos","events_url":"https://api.github.com/users/joepie91/events{/privacy}","received_events_url":"https://api.github.com/users/joepie91/received_events","type":"User","site_admin":false},"created_at":"2015-06-07T09:19:08Z","updated_at":"2015-06-07T09:19:08Z","body":"In case anybody ends up on this thread looking to use promises in Express *right now*, I've written an article [here](http://cryto.net/~joepie91/blog/2015/05/14/using-promises-bluebird-with-express/) on exactly that, using `express-promise-router`.\r\n\r\nMight be a useful stop-gap solution for some people, even if it doesn't completely implement them as described here :)"}}