title: Protractor - Wait for async promise before doing next
body: <p>First of all, I've already checked various post and blogs concerning that point and I still can't figure out how to make it correctly. </p>
  
  <p>I have tried many different combinaison of :</p>
  
  <ul>
  <li>browser wait</li>
  <li>protractor.controlFlow().execute</li>
  <li>protractor.controlFlow().await(</li>
  </ul>
  
  <p>...Still no success..</p>
  
  <p><strong>My problem</strong></p>
  
  <p>Within my beforeEach function, I'd like to call a protractor promise and wait for it to resolve before performing the rest of my code.</p>
  
  <p><strong>My Code</strong></p>
  
  <p>I've prepared this simple test for anyone willing to help me </p>
  
  <pre><code>describe('testAsync', function() {
  
    beforeEach(function() {
      console.log('beforeEach - step 1 ')
  
      browser.get("https://angularjs.org/");
      console.log('beforeEach - step 2 ')
      testFunc()
      console.log('beforeEach - after testFunc - step 3')
  
    });
  
    var testFunc = function(){
  
      console.log("testFunc - step 1")
  
      browser.wait(function() {
        var deferred = protractor.promise.defer();
        element(by.id('twitter-widget-1')).isPresent()
          .then(function (isPresent) {
            console.log("testFunc - step 2")
            deferred.fulfill(isPresent);
        });
        return deferred.promise;
      });
  
      console.log("testFunc - step 3")
  
    }
  
    it('test after BeforeEach', function() {
      console.log("Last trace")
    });
  
  });
  </code></pre>
  
  <p><strong>Current Output</strong></p>
  
  <pre><code>[launcher] Running 1 instances of WebDriver
  beforeEach - step 1
  beforeEach - step 2
  testFunc - step 1
  testFunc - step 3
  beforeEach - after testFunc - step 3
  testFunc - step 2
  Last trace
  </code></pre>
  
  <p><strong>Expected Output</strong></p>
  
  <pre><code>[launcher] Running 1 instances of WebDriver
  beforeEach - step 1
  beforeEach - step 2
  testFunc - step 1
  testFunc - step 2 // &lt;------  This is within the promise resolve
  testFunc - step 3
  beforeEach - after testFunc - step 3
  Last trace
  </code></pre>