Is it possible to return a value from the success method of $http.get in AngularJS?

Viewed 1103

I'm having a bit of a brainmelt with promises at the moment.

Given the following code:

$scope.getData = function(user) {

    return $http.post('/endpoint', "some post data", { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } })
        .success(function (response) {

            var myThing = { stuff: response.infoData };

            localStorageService.set('myData', { stuff: response.infoData });

            return myThing;
    });
};

Is the myThing object that is returned from the success callback:

  • returned as the result of the $scope.getData method, or
  • ignored and lost to nowhere?

From what I understand:

  • because $http.post is a promise, it resolves asynchronously and then runs the success method
  • when the success method is run, it returns the myThing object but this doesn't magically replace the entire $http.post promise itself

Is this correct? Is there something I'm missing, is there some benefit to returning objects in the success method, or is the return superfluous? (I'm well aware that this is code can be improved but I'm trying to find out specifically why someone would have written this, or if it was entirely a mistake)

1 Answers
Related