{"action":"created","issue":{"url":"https://api.github.com/repos/ruanyf/es6tutorial/issues/156","repository_url":"https://api.github.com/repos/ruanyf/es6tutorial","labels_url":"https://api.github.com/repos/ruanyf/es6tutorial/issues/156/labels{/name}","comments_url":"https://api.github.com/repos/ruanyf/es6tutorial/issues/156/comments","events_url":"https://api.github.com/repos/ruanyf/es6tutorial/issues/156/events","html_url":"https://github.com/ruanyf/es6tutorial/issues/156","id":140341954,"number":156,"title":"关于Promise#catch的解释","user":{"login":"sapjax","id":990255,"avatar_url":"https://avatars.githubusercontent.com/u/990255?v=3","gravatar_id":"","url":"https://api.github.com/users/sapjax","html_url":"https://github.com/sapjax","followers_url":"https://api.github.com/users/sapjax/followers","following_url":"https://api.github.com/users/sapjax/following{/other_user}","gists_url":"https://api.github.com/users/sapjax/gists{/gist_id}","starred_url":"https://api.github.com/users/sapjax/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sapjax/subscriptions","organizations_url":"https://api.github.com/users/sapjax/orgs","repos_url":"https://api.github.com/users/sapjax/repos","events_url":"https://api.github.com/users/sapjax/events{/privacy}","received_events_url":"https://api.github.com/users/sapjax/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2016-03-12T03:22:29Z","updated_at":"2016-03-12T03:48:45Z","closed_at":null,"body":"[Promise.prototype.catch()](http://es6.ruanyifeng.com/#docs/promise#Promise-prototype-catch)\r\n>```javascript\r\n// bad\r\npromise\r\n  .then(function(data) {\r\n    // success\r\n  }, function(err) {\r\n    // error\r\n  });\r\n>\r\n// good\r\npromise\r\n  .then(function(data) { //cb\r\n    // success\r\n  })\r\n  .catch(function(err) {\r\n    // error\r\n  });\r\n```\r\n上面代码中，第二种写法要好于第一种写法，理由是前者更接近同步的写法（try/catch）。\r\n\r\n最后的解释不是很准确，这里推荐用`catch`是因为：如果在`then`中(不是Promise执行过程中)抛出了一个错误，`then`的第二个参(数`onRejected`处理函数)是无法捕获到错误的，而`catch`的写法是可以捕获到的。"},"comment":{"url":"https://api.github.com/repos/ruanyf/es6tutorial/issues/comments/195652555","html_url":"https://github.com/ruanyf/es6tutorial/issues/156#issuecomment-195652555","issue_url":"https://api.github.com/repos/ruanyf/es6tutorial/issues/156","id":195652555,"user":{"login":"willin","id":1890238,"avatar_url":"https://avatars.githubusercontent.com/u/1890238?v=3","gravatar_id":"","url":"https://api.github.com/users/willin","html_url":"https://github.com/willin","followers_url":"https://api.github.com/users/willin/followers","following_url":"https://api.github.com/users/willin/following{/other_user}","gists_url":"https://api.github.com/users/willin/gists{/gist_id}","starred_url":"https://api.github.com/users/willin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/willin/subscriptions","organizations_url":"https://api.github.com/users/willin/orgs","repos_url":"https://api.github.com/users/willin/repos","events_url":"https://api.github.com/users/willin/events{/privacy}","received_events_url":"https://api.github.com/users/willin/received_events","type":"User","site_admin":false},"created_at":"2016-03-12T03:48:45Z","updated_at":"2016-03-12T03:48:45Z","body":"这个我个人感觉只要知道第二种写法好就行了，要说好的理由还能举出很多来，举个栗子：\r\n\r\n```javascript\r\n// bad\r\npromise.then(function(data1) {\r\n    // success1\r\n  }, function(err) {\r\n    // error\r\n  }).then(function(data2) {\r\n    // success2\r\n  }, function(err) {\r\n    // error\r\n    // 两者的错误捕获方法相同\r\n  });\r\n\r\n// good\r\npromise\r\n  .then(function(data1) { //cb\r\n    // success1\r\n  })\r\n  .then(function(data2) { //cb\r\n    // success2\r\n  })\r\n  .catch(function(err) {\r\n    // error\r\n    // 只catch了一次\r\n  });\r\n```"}}