title: Setting a timeout handler on a promise in angularjs
body: <p>I'm trying to set a timeout in my controller so that if a response isn't received in 250ms it should fail. I've set my unit test to have a timeout of 10000 so that this condition should be met,Can anyone point me in the right direction? ( EDIT I'm trying to achieve this without using the $http service which I know provides timeout functinality) </p>
  
  <p>(EDIT - my other unit tests were failing because I wasn't calling timeout.flush on them, now I just need to get the timeout message kicking in when an undefined promise is returned by promiseService.getPromise(). I've removed the early code from the question) .</p>
  
  <p>promiseService (promise is a test suite variable allowing me to use different behaviour for the promise in each test suite before apply, eg reject in one, success in another)</p>
  
  <pre><code>    mockPromiseService = jasmine.createSpyObj('promiseService', ['getPromise']);
      mockPromiseService.getPromise.andCallFake( function() {
          promise = $q.defer();
          return promise.promise;
      })
  </code></pre>
  
  <p>Controller function that's being tested - </p>
  
  <pre><code>$scope.qPromiseCall = function() {
      var timeoutdata = null;
      $timeout(function() {
          promise = promiseService.getPromise();
          promise.then(function (data) {
                  timeoutdata = data;
                  if (data == "promise success!") {
                      console.log("success");
                  } else {
                      console.log("function failure");
                  }
              }, function (error) {
                  console.log("promise failure")
              }
  
          )
      }, 250).then(function (data) {
          if(typeof timeoutdata === "undefined" ) {
              console.log("Timed out")
          }
      },function( error ){
          console.log("timed out!");
      });
  }
  </code></pre>
  
  <p>Test (normally I resolve or reject the promise in here but by not setting it I'm simulating a timeout)</p>
  
  <pre><code>it('Timeout logs promise failure', function(){
      spyOn(console, 'log');
      scope.qPromiseCall();
      $timeout.flush(251);
      $rootScope.$apply();
      expect(console.log).toHaveBeenCalledWith("Timed out");
  })
  </code></pre>