{"action":"created","issue":{"url":"https://api.github.com/repos/alexk111/ph-challenge/issues/7","labels_url":"https://api.github.com/repos/alexk111/ph-challenge/issues/7/labels{/name}","comments_url":"https://api.github.com/repos/alexk111/ph-challenge/issues/7/comments","events_url":"https://api.github.com/repos/alexk111/ph-challenge/issues/7/events","html_url":"https://github.com/alexk111/ph-challenge/issues/7","id":83657618,"number":7,"title":"A promise challenge...","user":{"login":"bramski","id":421781,"avatar_url":"https://avatars.githubusercontent.com/u/421781?v=3","gravatar_id":"","url":"https://api.github.com/users/bramski","html_url":"https://github.com/bramski","followers_url":"https://api.github.com/users/bramski/followers","following_url":"https://api.github.com/users/bramski/following{/other_user}","gists_url":"https://api.github.com/users/bramski/gists{/gist_id}","starred_url":"https://api.github.com/users/bramski/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bramski/subscriptions","organizations_url":"https://api.github.com/users/bramski/orgs","repos_url":"https://api.github.com/users/bramski/repos","events_url":"https://api.github.com/users/bramski/events{/privacy}","received_events_url":"https://api.github.com/users/bramski/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-06-01T19:34:53Z","updated_at":"2015-06-02T08:16:28Z","closed_at":null,"body":"Hey @alexk111 ,\r\n\r\nOnce you're done addressing my other questions.  Please consider the following code:\r\n```\r\nvar succeedAndPrintMessage, succeedIfEven, succeedIfEvenWithCatch;\r\n\r\nsucceedIfEven = function(number) {\r\n  var defer;\r\n  defer = $q.defer();\r\n  $timeout(function() {\r\n    if (number % 2 === 0) {\r\n      return defer.resolve(\"Even\");\r\n    } else {\r\n      return defer.reject(\"Odd\");\r\n    }\r\n  }, 100);\r\n  return defer.promise;\r\n};\r\n\r\nsucceedIfEvenWithCatch = function(number) {\r\n  return succeedIfEven(number)[\"catch\"](function(message) {\r\n    return console.log(\"Failed! \" + message);\r\n  });\r\n};\r\n\r\nsucceedAndPrintMessage = function(number) {\r\n  return succeedIfEvenWithCatch(number).then(function(message) {\r\n    return console.log(\"Number \" + number + \" is even!\");\r\n  });\r\n};\r\n\r\nsucceedAndPrintMessage(2);\r\n\r\nsucceedAndPrintMessage(3);\r\n```\r\n\r\nRead it, think about it.  Then answer the following WITHOUT running the code:\r\n\r\n(1) What do you think this code prints?\r\n(2) What did the developer who wrote this code want it to print?\r\n\r\nLeave a response here with those two answers.\r\nThen run the code and answer the following:\r\n\r\n(1) Did the code print what you expected?\r\n(2) Did the code print what the original developer intended?\r\n\r\nIf you find a bug in this code through this exercise and answering these questions, please post a correct solution below and explain what the issues were.\r\n\r\nCheers."},"comment":{"url":"https://api.github.com/repos/alexk111/ph-challenge/issues/comments/107854803","html_url":"https://github.com/alexk111/ph-challenge/issues/7#issuecomment-107854803","issue_url":"https://api.github.com/repos/alexk111/ph-challenge/issues/7","id":107854803,"user":{"login":"alexk111","id":4675984,"avatar_url":"https://avatars.githubusercontent.com/u/4675984?v=3","gravatar_id":"","url":"https://api.github.com/users/alexk111","html_url":"https://github.com/alexk111","followers_url":"https://api.github.com/users/alexk111/followers","following_url":"https://api.github.com/users/alexk111/following{/other_user}","gists_url":"https://api.github.com/users/alexk111/gists{/gist_id}","starred_url":"https://api.github.com/users/alexk111/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alexk111/subscriptions","organizations_url":"https://api.github.com/users/alexk111/orgs","repos_url":"https://api.github.com/users/alexk111/repos","events_url":"https://api.github.com/users/alexk111/events{/privacy}","received_events_url":"https://api.github.com/users/alexk111/received_events","type":"User","site_admin":false},"created_at":"2015-06-02T08:16:28Z","updated_at":"2015-06-02T08:16:28Z","body":"> If you find a bug in this code through this exercise and answering these questions, please post a correct solution below and explain what the issues were.\r\n\r\nThe reason of the problem is the wrong order of the then/catch calls. The provided code builds the succeedIfEven.catch(..).then(..) chain. When it catches the 'odd' error, it continues the execution. To fix it, the chain should look like succeedIfEven.then().catch() . As the catch() is just a shorthand for then(undefined, function(){}) we may simplify the chain and write it using a single then(). So here is the fixed code:\r\n\r\n```\r\nvar succeedAndPrintMessage, succeedIfEven, succeedIfEvenWithCatch;\r\n\r\nsucceedIfEven = function(number) {\r\n  var defer;\r\n  defer = $q.defer();\r\n  $timeout(function() {\r\n    if (number % 2 === 0) {\r\n      return defer.resolve(\"Even\");\r\n    } else {\r\n      return defer.reject(\"Odd\");\r\n    }\r\n  }, 100);\r\n  return defer.promise;\r\n};\r\n\r\nsucceedAndPrintMessage = function(number) {\r\n  return succeedIfEven(number).then(function(message) {\r\n    return console.log(\"Number \" + number + \" is even!\");\r\n  }, function(message) {\r\n    return console.log(\"Failed! \" + message);\r\n  });\r\n};\r\n\r\nsucceedAndPrintMessage(2);\r\n\r\nsucceedAndPrintMessage(3);\r\n\r\n```"}}