{"action":"closed","issue":{"url":"https://api.github.com/repos/webdriverio/webdriverio/issues/395","labels_url":"https://api.github.com/repos/webdriverio/webdriverio/issues/395/labels{/name}","comments_url":"https://api.github.com/repos/webdriverio/webdriverio/issues/395/comments","events_url":"https://api.github.com/repos/webdriverio/webdriverio/issues/395/events","html_url":"https://github.com/webdriverio/webdriverio/issues/395","id":54196861,"number":395,"title":"Chaining promises","user":{"login":"Dremora","id":242202,"avatar_url":"https://avatars.githubusercontent.com/u/242202?v=3","gravatar_id":"","url":"https://api.github.com/users/Dremora","html_url":"https://github.com/Dremora","followers_url":"https://api.github.com/users/Dremora/followers","following_url":"https://api.github.com/users/Dremora/following{/other_user}","gists_url":"https://api.github.com/users/Dremora/gists{/gist_id}","starred_url":"https://api.github.com/users/Dremora/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Dremora/subscriptions","organizations_url":"https://api.github.com/users/Dremora/orgs","repos_url":"https://api.github.com/users/Dremora/repos","events_url":"https://api.github.com/users/Dremora/events{/privacy}","received_events_url":"https://api.github.com/users/Dremora/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/webdriverio/webdriverio/labels/Bug","name":"Bug","color":"e10c02"},{"url":"https://api.github.com/repos/webdriverio/webdriverio/labels/Project+Related","name":"Project Related","color":"ffffff"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":4,"created_at":"2015-01-13T14:04:11Z","updated_at":"2015-01-15T14:47:40Z","closed_at":"2015-01-15T14:47:40Z","body":"I've recently started using Webdriver.io, and one of the first issues I've faced is how to reuse common functionality between tests.\r\n\r\nLet's say I have a panel in my application which I want to open/close in numerous places across the test suite. In a synchronous world, one would extract the code into a function called `togglePanel()`. However, let's say there is a short animation performed to transition panel from closed to opened state and backwards. The operation becomes asynchronous (as, generally speaking, would be most UI-related operations). In a node.js world, one would resolve this by relying on callbacks: `togglePanel(cb)`.\r\n\r\nHowever, in case of Webdriver.io, it seems that the only *documented* way to have shared asynchronous code is to create a custom command. Registering a custom command essentially makes it global, being a property of the `client` object. It's possible to put commands into different files, however, they have to bear unique names as they will share the same namespace.\r\n\r\nThere is one trick I thought could work. Each command returns a promise, so I tried to do something quite common in the promise world: return another promise from the `.then` handler. Consider the following code:\r\n\r\n```javascript\r\nfunction togglePanel () {\r\n  return client\r\n    .click('#panel button.toggle')\r\n    .pause(500)\r\n}\r\n```\r\n(this block is running within a Mocha test):\r\n```javascript\r\n...\r\nclient\r\n  .url('http://localhost:3000/')\r\n  .then(togglePanel)\r\n  .then(togglePanel)\r\n  .then(togglePanel)\r\n  .call(done)\r\n```\r\n\r\nWhat happens is panel gets properly toggled twice, but as soon as the 3rd toggle begins, `done()` executes immediately. It doesn't matter which command I use instead of `call` - they all seem to execute in parallel with the last `then()` handler.\r\n\r\nI understand there might have been no intention to make this work, but I'd like to be able to chain promises as shown above (which would be Promises/A+-compliant) or have something similar for modularizing my code without polluting global `client` object."}}