-1

I'm just trying resolving promises from a factory like this:

.config(['$routeProvider', '$locationProvider', '$httpProvider', 'SoundRTCProvider', function ($routeProvider, $locationProvider, $httpProvider, SoundRTCProvider) {

    $routeProvider
      .when('/',{
        templateUrl:'views/home/index.html',
        controller:'Home',
        resolve: {
          load: function (configFactory) {
            return configFactory.loadAll();
          }
        }
      });
});

then :

 .factory('configFactory',['$rootScope', '$q', '$location', '$anchorScroll', '$routeParams', 'ngDialog', 'cacheService', 'sessionFactory', 'geoFactory', 'instrumentsFactory', 'genresFactory', 'langFactory', 'usersFactory', 'contactsFactory', function ($rootScope, $q, $location, $anchorScroll, $routeParams, ngDialog, cacheService, sessionFactory, geoFactory, instrumentsFactory, genresFactory, langFactory, usersFactory, contactsFactory) {
    var initConfig = function () {
      var deferred = $q.defer();
      /*CONFIG PARAMS*/
      $rootScope.config = {};

      $rootScope.config.appWs = '';
      $rootScope.config.appName = 'Dunno';

      deferred.resolve('go');
      return deferred.promise();
    };

    var initUserSession = function () {
      var deferred = $q.defer();
      /*----- INIT USER SESSION ---*/
      $rootScope.session = {};
      /*RELOAD SESSION if logged*/
      if(sessionFactory.get('idUser')){
        usersFactory.getMyProfile().then(function(results){
          sessionFactory.initSession(results.data);
          deferred.resolve();
        });
      }
      return deferred.promise();
    };

    var initGravatar = function () {
      var deferred = $q.defer();
      /*------- INIT GRAVATARS ------*/
      $rootScope.gravatar = {};
      deferred.resolve();
      return deferred.promise();
    };

    var initLang = function () {
      var deferred = $q.defer();
      /*------LANGUAGE---------*/
      $rootScope.userLang = 'en_EN';
      $rootScope.lang = {};//get key and value from here in views

      //If user lang doesn't exists yet
      if(cacheService.isExpired('appLang')) {

        langFactory.getAll($rootScope.userLang)
         .then(function (response) {

              if(angular.isObject(response.data) && Object.keys(response.data).length > 0) {

                cacheService.put('appLang',response.data,$rootScope.config.minChacheTime);
                $rootScope.lang = cacheService.get('appLang');

              } else {

                $rootScope.lang = cacheService.get('appLang');
              }

              deferred.resolve();
            });

      } else {

        deferred.resolve();
        $rootScope.lang = cacheService.get('appLang');

      }
      return deferred.promise();
    };

    var initGenres = function () {
      var deferred = $q.defer();
      /*-------GENRES-------*/
      if(cacheService.isExpired('appGenres')) {

        genresFactory.getAll()
        .then(function (response) {

          if(angular.isObject(response.data) && Object.keys(response.data).length > 0) {

            cacheService.put('appGenres',response.data,$rootScope.config.minChacheTime);
            $rootScope.config.appGenres = cacheService.get('appGenres');

          } else {

            $rootScope.config.appGenres = cacheService.get('appGenres');
          }
          deferred.resolve();
        });

      } else {
        deferred.resolve();
        $rootScope.config.appGenres = cacheService.get('appGenres');
      }
      return deferred.promise();
    };
    var initInstruments = function () {
      var deferred = $q.defer();
      /*------INSTRUMMENTS------*/
      if(cacheService.isExpired('appInstruments')) {

        instrumentsFactory.getAll()
        .then(function (response) {

            if(angular.isObject(response.data) && Object.keys(response.data).length > 0) {

              cacheService.put('appInstruments',response.data,$rootScope.config.minChacheTime);
              $rootScope.config.appInstruments = cacheService.get('appInstruments');

            } else {

              $rootScope.config.appInstruments = cacheService.get('appInstruments');
            }

            deferred.resolve();
          });

      } else {
        deferred.resolve();
        $rootScope.config.appInstruments = cacheService.get('appInstruments');
      }
      return deferred.promise();
    };
    var initGeo = function () {
      var deferred = $q.defer();
      /*-------GEO----------*/
      if(cacheService.isExpired('appGeo')) {

        geoFactory.getAll()
         .then(function (response) {

          if(angular.isObject(response.data) && Object.keys(response.data).length > 0) {

            cacheService.put('appGeo',response.data,$rootScope.config.minChacheTime);
            $rootScope.config.appGeo = cacheService.get('appGeo');

          } else {

            $rootScope.config.appGeo = cacheService.get('appGeo');

          }
          deferred.resolve();
        });
      } else {
        deferred.resolve();
        $rootScope.config.appGeo = cacheService.get('appGeo');
      }
      return deferred.promise();
    };

    var initUserContacts = function () {
      var deferred = $q.defer();
      /*CONTACTS*/
      $rootScope.contacts = {
        approved: [],
        pending: []
      };
      if(sessionFactory.get('idUser')){
        contactsFactory.getMine().then(function (res) {
          if(angular.isArray(res.data) && res.data.length > 0) {

            for(var i in res.data) {

              //set all my pending contacts
              if(res.data[i].approved !== 1) {

                $rootScope.contacts.pending.push(res.data[i].idContact);

              } else {
                 //set all my contacts
                $rootScope.contacts.approved.push(res.data[i].idContact);

              }

            }
          }
          deferred.resolve();
        });
      } else {
        deferred.resolve();
      }
      return deferred.promise();
    };
    var initLayout = function () {
      var deferred = $q.defer();
      /*LAYOUT*/
      $rootScope.layout = {
        loading:true,
        userMenu:false
      };

      deferred.resolve();
      return deferred.promise();
    };

    var initScroll = function () {
      var deferred = $q.defer();
      //Make app scroll to top by default while navigating or use #anchor in url like http://sitess.com/someroute/?scrollTo=<:idelement:> to make it scroll to that element :id:
      $location.hash($routeParams.scrollTo);
      $anchorScroll();

      deferred.resolve();
      return deferred.promise();
    };

    var initDialog = function () {
      var deferred = $q.defer();
      //Close any dialog
      ngDialog.close();

      deferred.resolve();
      return deferred.promise();
    };

    var loadAll = function () {
      var deferred = $q.defer();

      initConfig()
      .then(function(){
        return initUserSession();
      }).then(function () {
        return initLang();
      }).then(function () {
        return initGenres();
      }).then(function () {
        return initGeo();
      }).then(function () {
        return initInstruments();
      }).then(function () {
        return initUserContacts();
      }).then(function () {
        return initGravatar();
      }).then(function () {
        return initLayout();
      }).then(function () {
        return initScroll();
      }).then(function () {
        return initDialog();
      }).then(function () {

        $rootScope.$on('loading:end', function(){
          $rootScope.layout.loading = false;
        });

        $rootScope.$on('loading:progressing', function (){
          $rootScope.layout.loading = true;
        });

        $rootScope.$on('$locationChangeError', function () {
          //hide loading gif
          $rootScope.layout.loading = false;
          //hide loading gif
          $rootScope.layout.userMenu = false;
        });

        //all done with configs
        deferred.resolve();
      });

      return deferred.promise();
    };

    return {
      initConfig:initConfig,
      initUserSession:initUserSession,
      initLang:initLang,
      initGenres:initGenres,
      initGeo:initGeo,
      initGravatar:initGravatar,
      initInstruments:initInstruments,
      initUserContacts:initUserContacts,
      initLayout:initLayout,
      initScroll:initScroll,
      initDialog:initDialog,
      loadAll:loadAll
    };
  }]);

What's up why do i get Error in console : TypeError: object is not a function at initConfig (assets/js/factories.js:35:23)

that means the first return deferred.promise(); is generating the error, i can't understand whats wrong, any help appriciated thanks

itsme
  • 48,972
  • 96
  • 224
  • 345

1 Answers1

1

You are trying to execute a normal object as a function (which is what the error means). It should be: return deferred.promise; instead of: return deferred.promise();.

Florian Margaine
  • 58,730
  • 15
  • 91
  • 116
thomastuts
  • 3,459
  • 3
  • 20
  • 28
  • 1
    gosh right :/ thanks mate going to accept your answer in few minutes – itsme May 19 '14 at 09:18
  • 3
    @BenjaminGruenbaum just sometimes try to understand that not all are genius and sometimes needs some little support also if documentation is clear ;) – itsme May 19 '14 at 09:29
  • Also, your code is _full_ of the deferred anti pattern and promise abuse... I didn't downvote this answer because it is trivial. I downvoted it because it provides __bad advice__, if you follow it, you'll accumulate technical debt. – Benjamin Gruenbaum May 19 '14 at 09:34
  • 1
    @BenjaminGruenbaum i mean sometimes happens i would delete the answer cause its really a stupid question actually but i prefer to accept the answer cause thomastuts deserves it nope? – itsme May 19 '14 at 09:35
  • 1
    @BenjaminGruenbaum i know you didn't downvote don't worry you know me i know you :D you a nice person – itsme May 19 '14 at 09:35
  • @sbaaaang no, this is actually a really bad answer - but then again the question is really bad too... it's a massive code dump which concludes with "I know where the problem is" - which you say "that means the first return deferred.promise(); is generating the error,", had you googled that mistake you would have found an SO error that explains how to solve it. – Benjamin Gruenbaum May 19 '14 at 09:36
  • 1
    actually voted to close it anyway! ;) – itsme May 19 '14 at 09:36
  • I _did_ downvote. It _is_ a bad question, and I think you know me good enough to know I don't downvote for no reason, the quality of the question _and_ answers is really low here. – Benjamin Gruenbaum May 19 '14 at 09:36
  • 1
    man please no time for arguing ;P sometimes happens i'm newbie on angular – itsme May 19 '14 at 09:37
  • 1
    ah ok for downvote no problem i love you anyway! ehehe – itsme May 19 '14 at 09:37
  • @sbaaaang I'm not arguing, for the 1000th time I'm trying to _help_ you write better questions and use angular better. Please consider reading https://github.com/petkaantonov/bluebird/wiki/Promise-anti-patterns#the-deferred-anti-pattern – Benjamin Gruenbaum May 19 '14 at 09:37
  • 1
    sure i try my best man :/ i'll try to keep an eyes everywher ;P – itsme May 19 '14 at 09:37