I am confused between Angular JS deferred and $q. I found this SO Question that explains the difference between $q.defer()
and $q
.It explains
$q.reject is a shortcut to create a deferred and then reject it immediately
So $q.reject()
must be equal to
var deferred = $q.defer(); deferred.reject()
, if not please explain the actual difference between the two.
But in my case, $q.reject()
is working, but deffered.reject()
is not working. Also we need to return rejected promised like $q.reject()
but not deferred.reject()
. I have seen examples where there is no return on deffered.reject()
This is the code
var deferred = $q.defer();
myService.getData()
.then(function(response){
deferred.notify('Just a notification');
deferred.reject('rejected');
})
.then(function(response) {
console.log('done');
}, function(response) {
console.log('rejected');
})
This is not working, but when I replaced deferred.reject
with $q.reject()
, the promise has been rejected and the control is moved to the error function of the followed then block.
Any help is greatly appreciated. Thanks in advance.
.then(function (response...
onto the promise that.getData()
has returned, not the promise thatdeferred
has return. Why should this work? it doesn't make sense. – Prod