title: Reuse of functions
body: <p>I think I know the theory behind the solution but I am having trouble implementing it. Consider following piece of code:</p>
  
  <pre><code>    this.selectFirstPassiveService = function () {
      getFirstPassiveService().element(by.tagName('input')).click();
  }
  
  this.clickAddTask = function () {
      getFirstActiveService().element(by.tagName('a')).click();
  }
  
  this.selectTask = function () {
      getFirstActiveService()
          .element(by.tagName('option'))
          .$('[value="0"]')
          .click();
  }
  
  this.saveTask = function () {
      getFirstActiveService().element(by.name('taskForm')).submit();
  }
  
  getFirstActiveService = function () {
      return services.filter(function (elem) {
          return elem.getAttribute('class').then(function (attribute) {
              return attribute === 'service active ';
          });
      }).first();
  }
  
  getFirstPassiveService = function () {
      return services.filter(function (elem) {
          return elem.getAttribute('class').then(function (attribute) {
              return attribute === 'service passive ';
          });
      }).first();
  }
  </code></pre>
  
  <p>};</p>
  
  <p>To minimalize code duplication, I created two functions:
  * getFirstActiveService()
  * getFirstPassiveService()</p>
  
  <p>My spec goes as follows:</p>
  
  <pre><code>    it('Select service', function () {
        servicePage.selectFirstPassiveService();
        servicePage.clickAddTask();
        servicenPage.selectTask()();
      });
  </code></pre>
  
  <p>Both clickAddTask() and selectTask() use the function called getFirstActiveService(). Everything runs fine in clickAddTask() but when I use the function in selectTask(), some elements (which are present) cannot be found by protractor. </p>
  
  <p>Here goes my theory, every command in getFirstActiveService() is queued in the control flow when the function is called in clickAddTask() and is then executed. When reusing the function in selectTask() the commands aren't queued, the instance created in clickAddTask() is used and therefore, some elements cannot be found since the DOM has changed since then.</p>
  
  <p>Now first question: Is my theory correct?
  Second question: How can I fix this?</p>
  
  <p>Thanks in advance!
  Cheers</p>