I'm struggling to understand the difference between Angular JS deferred and $q. I recently came across this SO Question that delves into the variance between $q.defer()
and $q
. According to the post:
$q.reject serves as a quick way to create a deferred object and immediately reject it.
So logically, $q.reject()
should equate to
var deferred = $q.defer(); deferred.reject()
. If this is not the case, please elucidate the actual discrepancy between the two.
However, in my scenario, $q.reject()
is functioning properly, while deferred.reject()
is not. Additionally, it seems necessary to return a rejected promise using $q.reject()
and not deferred.reject()
. I've noticed instances where deferred.reject()
lacks a return statement.
Here is the specific code snippet:
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');
})
Despite the above code not achieving the desired outcome, substituting deferred.reject
with $q.reject()
successfully rejects the promise and directs the control flow to the error function of the subsequent then block.
Any assistance on this matter would be greatly appreciated. Thank you in advance.