2
\$\begingroup\$

I'm kinda new to javascript and have question about it. Now my practical project is growing a bit bigger. I got the following if else statement. Is it possible to refactor this ? Should i use an use case or are there other options or will this decrease the loading speed of the application.

(function () {
 'use strict';
 angular
 .module('app')
 .factory('HttpResponseInterceptor', HttpResponseInterceptor);
 HttpResponseInterceptor.$inject = ['$q','$location'];
 function HttpResponseInterceptor($q,$location) {
 return {
 response: function(response){
 if (response.status === 401) {
 }
 return response || $q.when(response);
 },
 responseError: function(rejection) {
 if(rejection.status ===401 && rejection.config.url === "user") {
 console.log("Check")
 }
 else if (rejection.status ===401 && rejection.config.url == "l18n/fr_FR.json")
 {
 console.log("Check2")
 }
 else if (rejection.status ===401 && rejection.config.url == "l18n/nl_NL.json")
 {
 console.log("Check2")
 }
 else if (rejection.status ===401 && rejection.config.url == "l18n/de_DE.json")
 {
 console.log("Check2")
 }
 else if (rejection.status ===401 && rejection.config.url == "l18n/es_ES.json")
 {
 console.log("Check2")
 }
 else if (rejection.status ===401 && rejection.config.url == "l18n/en_US.json")
 {
 console.log("Check2")
 }
 else
 {
 $location.path('/login');
 }
 /* if (rejection.status === 401 && rejection.config.url !== "user") {
 $location.path('/login');
 }*/
 return $q.reject(rejection);
 }
 };
 }
})();
200_success
145k22 gold badges190 silver badges478 bronze badges
asked Oct 18, 2015 at 8:50
\$\endgroup\$
1

2 Answers 2

5
\$\begingroup\$

I'd actually go a bit further than janos. He is perfectly right in that by repeatedly checking rejection.status, you're repeating yourself in code. Repeatedly checking the variable against multiple values makes this a perfect candidate for a switch statement:

responseError: function(rejection) {
 if (rejection.status === 401) {
 switch (rejection.config.url) {
 case "user":
 console.log("Check");
 break;
 case "l18n/fr_FR.json":
 case "l18n/nl_NL.json":
 case "l18n/de_DE.json":
 case "l18n/es_ES.json":
 case "l18n/en_US.json":
 console.log("Check2");
 break;
 default:
 $location.path('/login');
 break;
 }
 }
 return $q.reject(rejection); 
}
answered Oct 19, 2015 at 11:36
\$\endgroup\$
0
2
\$\begingroup\$

Don't repeat yourself. The repeated checks on rejection.status ===401 are clearly pointless. Rewrite in a way to check this condition only once, for example by nesting the other conditions:

if (rejection.status === 401) {
 if (rejection.config.url === "user") {
 console.log("Check")
 }
 else if (rejection.config.url == "l18n/fr_FR.json")
 {
 console.log("Check2")
 }
 else if (rejection.config.url == "l18n/nl_NL.json")
 {
 console.log("Check2")
 }
 //
 // ... more checks
 //
 else
 {
 $location.path('/login');
 }
} else
{
 $location.path('/login');
}
answered Oct 18, 2015 at 9:07
\$\endgroup\$
1
  • 2
    \$\begingroup\$ You're better off with a guard clause at the top to handle the non-401 case. That way the large if...else can be handled without nested indentation. \$\endgroup\$ Commented Oct 19, 2015 at 1:46

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.