0
initUserProfile: function() {

  return $http.get('myapi/user/profil').
  then(function(response) {
    current_user = response.data;
    if (current_user.profilpicture) {
      current_user.profilpicture = $rootScope.baseURL + "/images/users/" + current_user.username + "/" + current_user.profilpicture;
    }
  }, function(err) {
    // log error
  });
};

I want this to return a promise, here it's the case but it's the $http success promise. How can i do to return a promise when the changes inside the then() are completed ?

charlietfl
  • 170,828
  • 13
  • 121
  • 150
Nuzzob
  • 371
  • 1
  • 4
  • 23

2 Answers2

2

Just return current_user in then() and it will be available as argument of next then() in the chain

initUserProfile: function() {

  return $http.get('myapi/user/profil').
  then(function(response) {
    current_user = response.data;
    if (current_user.profilpicture) {
      current_user.profilpicture = $rootScope.baseURL + "/images/users/" + current_user.username + "/" + current_user.profilpicture;
    }

     return current_user;


  }).catch(function(err) {
    // log error
  });
};

In controller:

myService.initUserProfile().then(function(current_user){
    $scope.user = current_user;
})
charlietfl
  • 170,828
  • 13
  • 121
  • 150
0
    initUserProfile: function ()
    {

    var defer=$q.defer();

    $http.get('myapi/user/profil').
            then(function (response) {
            current_user = response.data;
                    if (current_user.profilpicture)
                    {
                        current_user.profilpicture = $rootScope.baseURL + "/images/students/" + current_user.username + "/" + current_user.profilpicture;

                    //here resolve
                    defer.resolve();

                    }else{
                     defer.reject();
                    }


                }, function (err) {
                    // log error
                    defer.reject();
                });


    //retun promise
    return defer.promise;
    };
developer033
  • 24,267
  • 8
  • 82
  • 108
Maciej Sikora
  • 19,374
  • 4
  • 49
  • 50
  • 1
    This is considered an anti pattern...no need for $q here at all – charlietfl Jul 21 '16 at 17:14
  • question is how to return promise when code in then is completed - so this is answer. why anti pattern, if we need method to be resolved after some asynchronous code. – Maciej Sikora Jul 21 '16 at 17:17
  • 1
    because `$http` already returns a promise... http://stackoverflow.com/questions/23803743/what-is-the-explicit-promise-construction-antipattern-and-how-do-i-avoid-it – charlietfl Jul 21 '16 at 17:19
  • i know that, but question was to get promise after code in then, not promise from http i think author knows that http returns promise. I should not have had minus for that answer. Using $q if we can use promise from http has no sense, but use $q after some code or in if has sense. – Maciej Sikora Jul 21 '16 at 17:22
  • don't think you understand that when you return inside then() a promise is passed down chain. You didn't resolve with any data either so controller won't get anything – charlietfl Jul 21 '16 at 17:25