{"action":"created","issue":{"url":"https://api.github.com/repos/anvilresearch/connect/issues/183","labels_url":"https://api.github.com/repos/anvilresearch/connect/issues/183/labels{/name}","comments_url":"https://api.github.com/repos/anvilresearch/connect/issues/183/comments","events_url":"https://api.github.com/repos/anvilresearch/connect/issues/183/events","html_url":"https://github.com/anvilresearch/connect/issues/183","id":102294331,"number":183,"title":"Should we use Promises?","user":{"login":"vsimonian","id":9868643,"avatar_url":"https://avatars.githubusercontent.com/u/9868643?v=3","gravatar_id":"","url":"https://api.github.com/users/vsimonian","html_url":"https://github.com/vsimonian","followers_url":"https://api.github.com/users/vsimonian/followers","following_url":"https://api.github.com/users/vsimonian/following{/other_user}","gists_url":"https://api.github.com/users/vsimonian/gists{/gist_id}","starred_url":"https://api.github.com/users/vsimonian/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vsimonian/subscriptions","organizations_url":"https://api.github.com/users/vsimonian/orgs","repos_url":"https://api.github.com/users/vsimonian/repos","events_url":"https://api.github.com/users/vsimonian/events{/privacy}","received_events_url":"https://api.github.com/users/vsimonian/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/anvilresearch/connect/labels/needs%20investigation","name":"needs investigation","color":"fef2c0"},{"url":"https://api.github.com/repos/anvilresearch/connect/labels/p2","name":"p2","color":"fbca04"},{"url":"https://api.github.com/repos/anvilresearch/connect/labels/question","name":"question","color":"cc317c"},{"url":"https://api.github.com/repos/anvilresearch/connect/labels/refactor","name":"refactor","color":"fad8c7"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-08-21T04:14:07Z","updated_at":"2015-08-21T05:40:16Z","closed_at":null,"body":"Promises have the potential to significantly clear up a lot of async code that we have laying about. I think it's worth trying it out on a small scale and seeing whether or not it would be a good fit for the codebase we have."},"comment":{"url":"https://api.github.com/repos/anvilresearch/connect/issues/comments/133295077","html_url":"https://github.com/anvilresearch/connect/issues/183#issuecomment-133295077","issue_url":"https://api.github.com/repos/anvilresearch/connect/issues/183","id":133295077,"user":{"login":"vsimonian","id":9868643,"avatar_url":"https://avatars.githubusercontent.com/u/9868643?v=3","gravatar_id":"","url":"https://api.github.com/users/vsimonian","html_url":"https://github.com/vsimonian","followers_url":"https://api.github.com/users/vsimonian/followers","following_url":"https://api.github.com/users/vsimonian/following{/other_user}","gists_url":"https://api.github.com/users/vsimonian/gists{/gist_id}","starred_url":"https://api.github.com/users/vsimonian/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vsimonian/subscriptions","organizations_url":"https://api.github.com/users/vsimonian/orgs","repos_url":"https://api.github.com/users/vsimonian/repos","events_url":"https://api.github.com/users/vsimonian/events{/privacy}","received_events_url":"https://api.github.com/users/vsimonian/received_events","type":"User","site_admin":false},"created_at":"2015-08-21T05:40:16Z","updated_at":"2015-08-21T05:40:16Z","body":"An example with some sample code similar to some stuff we have in Anvil Connect\r\n\r\n**Without Promises:**\r\n\r\n```js\r\nfunction someMiddleware(req, res, next) {\r\n\r\n  Model.doSomeWork({ opt: 'val' }, function (err, model) {\r\n    if (err) { return next(err); }\r\n    if (!model) { return next(new Error('Invalid request')); }\r\n\r\n    model.doSomethingElse(function (err, result) {\r\n      if (err) { return next(err); }\r\n      if (!result) { return next(new Error('Invalid options')); }\r\n\r\n      if (result.x === 1) {\r\n\r\n        model.doAnotherThing({ opt: result.y }, function (err, attr) {\r\n          if (err) { return next(err); }\r\n\r\n          if (attr) {\r\n            res.render('view', { data: result.attrs[attr] });\r\n          } else {\r\n            res.render('view', { data: result });\r\n          }\r\n        });\r\n\r\n      } else {\r\n        res.render('view', { data: result });\r\n      }\r\n    });\r\n  });\r\n\r\n}\r\n```\r\n\r\n**With Promises:**\r\n\r\n```js\r\nfunction someMiddleware(req, res, next) {\r\n\r\n  Model.doSomeWork({ opt: 'val' }).then(function (model) {\r\n    if (!model) { throw new Error('Invalid request'); }\r\n    return model.doSomethingElse();\r\n\r\n  }).then(function (result) {\r\n    if (!result) { throw new Error('Invalid options'); }\r\n\r\n    if (result.x === 1) {\r\n\r\n      return model.doAnotherThing({ opt: result.y }).then(function (attr) {\r\n        return attr ? result.attrs[attr] : result;\r\n      });\r\n\r\n    } else {\r\n      return result;\r\n    }\r\n\r\n  }).then(function (data) {\r\n    res.render('view', { data: data });\r\n  }).catch(function (err) {\r\n    next(err);\r\n  });\r\n\r\n}\r\n```"}}