jensengar
jensengar

Reputation: 6167

Reject promise in the chain

So this may be trivial, but I am doing some proof of concept stuff and trying to reject a promise in the middle of a promise chain, but I am not getting the results I would expect.

app.controller('MainCtrl', function($scope, $q) {
    var def = $q.defer();

    def.promise
    .then(testPromiseReject())
    .then(
      function(){
        console.log("SUCCESS")
      }, 
      function(){
        console.log("FAIL")
      });

    def.resolve();

    function testPromiseReject(action)
    {
      return $q.reject()
    }
  });

I think I am creating a promise that I initially resolve, but in the first then I have a function that I am trying to reject the rest of the promise chain. The above code prints "SUCCESS" to the console. Why is it not rejecting the rest of the chain?

Upvotes: 2

Views: 154

Answers (1)

Anthony Chu
Anthony Chu

Reputation: 37560

There's a problem with this line...

.then(testPromiseReject())

It just needs the ()s removed so it's not executed immediately...

.then(testPromiseReject)

Fiddle... http://jsfiddle.net/5LVEE/1/

Upvotes: 2

Related Questions