0

Is it possible that I cancel a promise without going to it's error scenario ?

var a = b.getPromise().
success(function(){
    //whoa
})
.error(function(){
    //no!!!
})

 b = {};
 b.getPromise = function(){
     return $q.defer().promise;
 }
sij
  • 1,307
  • 7
  • 18
  • 35
  • 2
    There is no `.success` or `.error` in a `$q` promise - there's `.then` and `.catch`. Also, there's no notion of "cancelling" of a promise. – New Dev Apr 16 '15 at 00:55
  • you can not cancel a promise but you can always stop propagation of promise depending on conditions – Ravi Sahu Apr 16 '15 at 02:03
  • See my answer [here](http://stackoverflow.com/questions/29525354/best-way-to-handle-early-returns-in-chains-of-promises/29558623#29558623) – Roamer-1888 Apr 16 '15 at 07:35
  • @NewDev yes there is, you can cancel a promise in Angular in some scenarios - for example in `$http` http://stackoverflow.com/questions/13928057/how-to-cancel-an-http-request-in-angularjs – Benjamin Gruenbaum Apr 16 '15 at 09:28
  • @BenjaminGruenbaum, yes, of course, but that is cancelling the task associated with the promise - the HTTP request in your example - which rejects the promise, not "cancelling the promise". I have a feeling that the OP meant to ask about `$http` (given reference to `.success`/`.error` and the cancellation), and I hoped he would correct it based on my first comment. – New Dev Apr 16 '15 at 16:00

1 Answers1

0

I don't think you can cancel Angular $q promises at all?

But if you don't want to call either handlers, you will have to return a promise that is forever pending. Not that I would recommend that…

A better idea would be to cancel so that the error path is activated, and you handle the cancellation case there explicitly.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375