{"action":"created","issue":{"url":"https://api.github.com/repos/segmentio/nightmare/issues/737","repository_url":"https://api.github.com/repos/segmentio/nightmare","labels_url":"https://api.github.com/repos/segmentio/nightmare/issues/737/labels{/name}","comments_url":"https://api.github.com/repos/segmentio/nightmare/issues/737/comments","events_url":"https://api.github.com/repos/segmentio/nightmare/issues/737/events","html_url":"https://github.com/segmentio/nightmare/issues/737","id":168044012,"number":737,"title":"Error on 'wait' after 'goto' status code check","user":{"login":"voidmonk","id":84814,"avatar_url":"https://avatars.githubusercontent.com/u/84814?v=3","gravatar_id":"","url":"https://api.github.com/users/voidmonk","html_url":"https://github.com/voidmonk","followers_url":"https://api.github.com/users/voidmonk/followers","following_url":"https://api.github.com/users/voidmonk/following{/other_user}","gists_url":"https://api.github.com/users/voidmonk/gists{/gist_id}","starred_url":"https://api.github.com/users/voidmonk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/voidmonk/subscriptions","organizations_url":"https://api.github.com/users/voidmonk/orgs","repos_url":"https://api.github.com/users/voidmonk/repos","events_url":"https://api.github.com/users/voidmonk/events{/privacy}","received_events_url":"https://api.github.com/users/voidmonk/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":3,"created_at":"2016-07-28T08:48:41Z","updated_at":"2016-07-29T22:57:50Z","closed_at":null,"body":"Hey guys, I'm trying to check the `goto` request's HTTP status code before evaluating the results:\r\n\r\n```node\r\nnightmare\r\n  .goto(url)\r\n  .then((gotoStatus) => {\r\n    if (!gotoStatus || !gotoStatus.code || gotoStatus.code != 200) {\r\n      // non-OK response code: halt nightmare and respond with error\r\n      nightmare.end();\r\n      console.log(\"HTTP error\");\r\n    }\r\n  })\r\n  .wait(250) // only wait on OK response for DOM & page JS to complete updates\r\n  .evaluate(function() {\r\n    // some eval code that returns results\r\n  })\r\n  .end()\r\n  .then(function(results) {\r\n    console.log(results);\r\n  })\r\n\r\n```\r\n\r\nBut I keep getting the error:\r\n\r\n```\r\n .wait(250)\r\n       ^\r\n\r\nTypeError: nightmare.goto(...).then(...).wait is not a function\r\n```\r\n\r\nAny idea why an error is thrown? It works if the `then` function (which checks the status code) after `goto` is removed."},"comment":{"url":"https://api.github.com/repos/segmentio/nightmare/issues/comments/236314044","html_url":"https://github.com/segmentio/nightmare/issues/737#issuecomment-236314044","issue_url":"https://api.github.com/repos/segmentio/nightmare/issues/737","id":236314044,"user":{"login":"Mr0grog","id":74178,"avatar_url":"https://avatars.githubusercontent.com/u/74178?v=3","gravatar_id":"","url":"https://api.github.com/users/Mr0grog","html_url":"https://github.com/Mr0grog","followers_url":"https://api.github.com/users/Mr0grog/followers","following_url":"https://api.github.com/users/Mr0grog/following{/other_user}","gists_url":"https://api.github.com/users/Mr0grog/gists{/gist_id}","starred_url":"https://api.github.com/users/Mr0grog/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Mr0grog/subscriptions","organizations_url":"https://api.github.com/users/Mr0grog/orgs","repos_url":"https://api.github.com/users/Mr0grog/repos","events_url":"https://api.github.com/users/Mr0grog/events{/privacy}","received_events_url":"https://api.github.com/users/Mr0grog/received_events","type":"User","site_admin":false},"created_at":"2016-07-29T22:57:50Z","updated_at":"2016-07-29T22:57:50Z","body":"Some notes:\r\n\r\n```js\r\n.then((gotoStatus) => {\r\n    if (!gotoStatus || !gotoStatus.code || gotoStatus.code != 200) {\r\n```\r\n\r\n`goto` *always* returns an object, whether there was success or error, so you can just `if (gotoStatus.code !== 200)`\r\n\r\nYou should also add a `.catch(error => { /* do something */ })` after the `.then()` handler in case the `goto` fails (e.g. you are offline, the server doesn't respond, the request times out, too many redirects, etc) and in case there are other errors in the rest of your code.\r\n\r\nThis last one is purely stylistic, but you can pull the last `.then()` up a level so you have less nesting.\r\n\r\nHere’s a slight revision that might be a bit more solid:\r\n\r\n```js\r\nnightmare\r\n  .goto(url)\r\n  .then(gotoStatus => {\r\n    if (gotoStatus.code !== 200) {\r\n      return Promise.reject(\"HTTP error\");\r\n    }\r\n    // OK response\r\n    return nightmare\r\n      .wait(250)\r\n      .evaluate(function() {\r\n        // some eval code that returns results\r\n      });\r\n  })\r\n  .then(result => console.log('Result:', result))\r\n  .catch(error => console.log('An error occurred:', error))\r\n  .then(_ => nightmare.end());\r\n```"}}
{"actor":{"display_login":"Mr0grog"}}