How to resolve this "unreachable code after return statement" warning?

I'm trying to learn the basics of angularjs and am working with the tutorial app and ng-js v1.6.6. Although the items show up (localhost), the browser shows a warning as seen in the screengrab. Chrome doesn't show the error-firefox does. Is the code okay or are there errors on line 17 / line 50?

   'use strict';

    angular.module('components.phone')
    .factory('Phone', ['$resource', function($resource,) {
    return $resource('components/phone/data/phones/:phoneId.json', {}, { 
        query: {
            method:'GET', 
            params: { phoneId:'phones' },
            isArray: true
            }
        }
      )    

     return factory;//<======LINE 17 -IS THE REST OF THE CODE 'UNREACHABLE'?
     }])

.service('PhoneService', ['$q','$http', '$resource', function($q, $http) {
    var service = { 
        get: function() {
            var deferred = $q.defer();

            $http.get('components/phone/data/phones/phones.json', { 
                params: { phoneId:'phones' } 
            })
            .then(function(response){
                deferred.resolve(response.data);
            })
            .catch(function(response){
                deferred.resolve(response);
            });

            return deferred.promise;
        },

        getAllPhones: function() {
            return $http.get('components/phone/data/phones/:phoneId.json', { 
                cache:   true }
            ).then(function(resp) {
                return resp.data;
            });
        },

        getPhone: function(phoneId) {
                for (i = 0; i < phones.length; i++) {
                    if(phones[i].id == phoneId){
                    this.phone = phones[i];
                     break; <==== THIS SYNTAX IS NOW CHANGED, THANKS@SYNCRO!


        return  $http.get('components/phone/data/phones/' +    $stateParams.phoneId + '.json')
                    .then(function(response) {
                        self.phone = response.data;

                    })
            }

            return service.getAllPhones().then(function (getPhone) {
                return selected.phone;
            })
        }
    }

    return service;
}])

.run(['$rootScope', '$state', '$stateParams',
    function ($rootScope, $state, $stateParams) {
        $rootScope.$state = $state;
        $rootScope.$stateParams = $stateParams;
    }  
])

Console error in FF

1 answer

  • answered 2018-01-11 21:02 Syncro

    In your getPhone method, if you return inside a for loop, the code after the for loop will be not executed, that's why your browser is alerting you with this warning message.

    You can see this message also in Chrome, you just have to change the Log priority in your Chrome DevTools.

    You can see that return problem in this little script that I wrote for you: https://jsfiddle.net/kmy7hq8q/

    To leave the loop you just have to use break, not return.

    if(phones[i].id == phoneId){
        this.phone = phones[i];
        break;
    }