What does "return $ q.reject (response)" do mean?

I'm a little confused by the statement

return $q.reject(response);

located inside the interceptor responseError.

I went through this article about webdeveasy and one on angular docs , but they did not help.

Here is my code (for reference):

(function () {
    "use strict";
    angular.module('xyz').factory('errorResponseInterceptor', ['$q', 'toaster', function ($q, toaster) {
        return {
            ...
            ...
            ...
            responseError: function (response) {
                ...
                ...
                //some logic here
                ...
                ...
                if (response.status == 500) {
                        toaster.error({ title: "", body: response.statusText });
                    return $q.reject(response);//what does this statement does and why do we need to put it here?
                }
                return response;
            }
        };
    }]);
}());

My question is:

  • Why should we write return $q.reject(response)?
  • How does this line affect the angular application (what does it do)?
+4
source share
1 answer

An object $qin Angular allows us to define a promise and handle the behavior associated with the promise. An example of how we can do this:

var q = $q.defer()

//do something
if(success){
  q.resolve()
} else {
  q.reject()
}

return q

, q. , , . .resolve() , . .reject() , .

, :

SomeFactory.getSomething().then(function(data){
  //gets called if promise resolves
}, function(error){
  //gets called if promise rejected
}

, , , , 500, , , .

responseError , . $q.reject(response), . - ( , ), , ​​ .

:

1- , , .

2- Angular , .

+4

Source: https://habr.com/ru/post/1629312/


All Articles