title: Jasmine async test using promises
body: <p>I am doing some Jasmine testing using angular promises and have a question related to timing. Found an answer here <a href="https://stackoverflow.com/questions/16081586/unit-test-promise-based-code-in-angular">Unit-test promise-based code in Angular</a>, but need clarification about how this works. Given that the <code>then</code> method is always handled in an asynchronous way how is the following test guaranteed to pass. Isn't there are risk that the <code>expect</code> will run ahead of the <code>then</code> block being executed and run the expect before the value has been assigned. Or... does the digest cycle guarantee that the value will be assigned before the expect runs. Meaning, a digest cycle will effectively behave like a blocking call that guarantees that the promises are all resolved before the code is allowed to proceed.</p>
  
  <pre><code>function someService(){
    var deferred = $q.defer();
    deferred.resolve(myObj); 
    return deferred.promise;
  } 
  
  it ('testing promise', function() {
    var res;
    var res2;
    someService().then(function(obj){
      res = "test";
    });
  
    someService().then(function(obj){
      res2 = "test2";
    });
  
    $rootScope.$apply(); 
    expect(res).toBe('test');
    expect(res2).toBe('test2');
  });
  </code></pre>