Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am a bit confused about what the statement

return $q.reject(response);

does inside the responseError interceptor.

I have gone through this article on webdeveasy and one on angular docs but they haven't helped.

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:

  1. Why do we need to write return $q.reject(response)?
  2. How does that line affect the angular app (what does it do)?
like image 813
Jenish Rabadiya Avatar asked Feb 18 '16 06:02

Jenish Rabadiya


People also ask

What is Q defer ()?

$q. defer() allows you to create a promise object which you might want to return to the function that called your login function.

How to use$ q in AngularJS?

$q constructor The $q implementation in AngularJS is now available in the same aspect as the native ES2015 Promise Object, therefore we can do this: let promise = $q((resolve, reject) => { if (/* some async task is all good */) { resolve('Success! '); } else { reject('Oops... something went wrong'); } }); promise.

What is $q in AngularJS?

$q is integrated with the $rootScope. Scope Scope model observation mechanism in AngularJS, which means faster propagation of resolution or rejection into your models and avoiding unnecessary browser repaints, which would result in flickering UI. Q has many more features than $q, but that comes at a cost of bytes.

What is promise in AngularJS?

Promises in AngularJS are provided by the built-in $q service. They provide a way to execute asynchronous functions in series by registering them with a promise object. {info} Promises have made their way into native JavaScript as part of the ES6 specification.


1 Answers

The $q object in Angular allows us to define a promise and handle the behaviour associated with a promise. An example of how we might do this is:

var q = $q.defer()

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

return q

What we are doing here is defining a promise object and assigning it to the variable q. Then we perform some operation and use the result of that to determine whether the promise returns successfully or not. Calling .resolve() on the promise object is indicative of the fact that the promise has returned correctly. By the same token calling .reject() on the promise is indicative of the fact that is has failed.

If we consider how we actually use a promise:

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

So in the example you have provided, we are checking to see if the response has a 500 error code, and if it does, we are rejecting the promise thus allowing us to handle the error.

In a responseError interceptor you have two choices with regards to what you return. If you return $q.reject(response) you are continuing the error handling chain of events. If you return anything else (generally a new promise or a response) you are indicating that the error has been recovered from and should be treated as a success.

With regards to your two points:

1- You need to write that line to indicate that the response should still be considered an error.

2- The effect on the angular app is that the error callback will be called instead of the success callback.

like image 69
Spacepotato Avatar answered Oct 24 '22 23:10

Spacepotato