{"action":"created","issue":{"url":"https://api.github.com/repos/webdriverio/webdriverio/issues/1054","labels_url":"https://api.github.com/repos/webdriverio/webdriverio/issues/1054/labels{/name}","comments_url":"https://api.github.com/repos/webdriverio/webdriverio/issues/1054/comments","events_url":"https://api.github.com/repos/webdriverio/webdriverio/issues/1054/events","html_url":"https://github.com/webdriverio/webdriverio/issues/1054","id":128603495,"number":1054,"title":"How to call old-style asynchronous function with Promise.","user":{"login":"NicolasPelletier","id":2111848,"avatar_url":"https://avatars.githubusercontent.com/u/2111848?v=3","gravatar_id":"","url":"https://api.github.com/users/NicolasPelletier","html_url":"https://github.com/NicolasPelletier","followers_url":"https://api.github.com/users/NicolasPelletier/followers","following_url":"https://api.github.com/users/NicolasPelletier/following{/other_user}","gists_url":"https://api.github.com/users/NicolasPelletier/gists{/gist_id}","starred_url":"https://api.github.com/users/NicolasPelletier/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/NicolasPelletier/subscriptions","organizations_url":"https://api.github.com/users/NicolasPelletier/orgs","repos_url":"https://api.github.com/users/NicolasPelletier/repos","events_url":"https://api.github.com/users/NicolasPelletier/events{/privacy}","received_events_url":"https://api.github.com/users/NicolasPelletier/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2016-01-25T18:18:02Z","updated_at":"2016-01-25T18:29:38Z","closed_at":"2016-01-25T18:29:38Z","body":"I am trying to call an old-style node function inside a chain of webdriverio promises.\r\n\r\nHere is a simplified example.\r\n```javascript\r\n// This simulate an async function on which I have no control.\r\nvar asyncFunc = function(done) {\r\n  setTimeout(done, 4000);\r\n};\r\nvar asyncIsDone = false;\r\n\r\nrequire('webdriverio')\r\n  .remote({ desiredCapabilities: { browserName: 'chrome' } })\r\n  .init()\r\n  .url('http://www.github.com')\r\n  .then(function() {\r\n    var df = this.defer;\r\n    asyncFunc(function() {\r\n      console.log('1. async function is done.');\r\n      asyncIsDone = true;\r\n      return df.resolve('abc123');\r\n    });\r\n    return df.promise;\r\n  })\r\n  .then(function(res) {\r\n    if (!asyncIsDone) {\r\n      console.log('2. We got here too early.');\r\n    }\r\n  })\r\n  .getTitle()\r\n  .then(function(title) {\r\n    console.log('3.', title);\r\n  })\r\n  .end();\r\n```\r\n\r\nIs there some way to execute an old-style function with callback inside a promise chain ? I found examples with 'q', but the promise returned does not allow me to use 'getTitle()' and other commands of webdriverio.\r\n\r\nI found the 'client.defer' and I thought it would work, but evidently, it does not. \r\n\r\nAny ideas ?\r\n\r\nThanks."},"comment":{"url":"https://api.github.com/repos/webdriverio/webdriverio/issues/comments/174611814","html_url":"https://github.com/webdriverio/webdriverio/issues/1054#issuecomment-174611814","issue_url":"https://api.github.com/repos/webdriverio/webdriverio/issues/1054","id":174611814,"user":{"login":"christian-bromann","id":731337,"avatar_url":"https://avatars.githubusercontent.com/u/731337?v=3","gravatar_id":"","url":"https://api.github.com/users/christian-bromann","html_url":"https://github.com/christian-bromann","followers_url":"https://api.github.com/users/christian-bromann/followers","following_url":"https://api.github.com/users/christian-bromann/following{/other_user}","gists_url":"https://api.github.com/users/christian-bromann/gists{/gist_id}","starred_url":"https://api.github.com/users/christian-bromann/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/christian-bromann/subscriptions","organizations_url":"https://api.github.com/users/christian-bromann/orgs","repos_url":"https://api.github.com/users/christian-bromann/repos","events_url":"https://api.github.com/users/christian-bromann/events{/privacy}","received_events_url":"https://api.github.com/users/christian-bromann/received_events","type":"User","site_admin":false},"created_at":"2016-01-25T18:29:38Z","updated_at":"2016-01-25T18:29:38Z","body":"Hi @NicolasPelletier ,\r\nyou should not use the internal command promise. Here is an example that works:\r\n\r\n```js\r\n// This simulate an async function on which I have no control.\r\nvar asyncFunc = function(done) {\r\n  setTimeout(done, 4000);\r\n};\r\nvar asyncIsDone = false;\r\n\r\nrequire('webdriverio')\r\n  .remote({ desiredCapabilities: { browserName: 'phantomjs' } })\r\n  .init()\r\n  .url('http://www.github.com')\r\n  .then(function() {\r\n    return new Promise(function(resolve) {\r\n      asyncFunc(function() {\r\n        console.log('1. async function is done.');\r\n        asyncIsDone = true;\r\n        resolve('abc123');\r\n      });\r\n    })\r\n  })\r\n  .then(function(res) {\r\n    if (!asyncIsDone) {\r\n      console.log('2. We got here too early.');\r\n    }\r\n  })\r\n  .getTitle()\r\n  .then(function(title) {\r\n    console.log('3.', title);\r\n  })\r\n  .end();\r\n```"}}