0

My controller has all the required dependencies injected.

 $scope.connect = function(url) {
    var defer = $q.defer();
    var promise = $http.get(url).then(function (response) {
         $timeout(function(){
                defer.resolve(response);
            },10000);
        defer.resolve(response);
        $scope.$apply();  //$rootScope.$apply();
    });
    return defer.promise;
 };

$scope.mymethod = function(){
    $scope.globalMydata[];
    console.log("before the http get");
    $scope.connect("MY_URL").then(function(data) {
         console.log("called!", data);
         console.log("INSIDE the http get");
         $scope.mydata = data;
         //$scope.globalMydata.push(data);            
    });
     console.log("after the http get ");
    //do some processing of the returned data here
    var dataHolder = $scope.mydata;
    return calculatedValue;//value from procesing

}

When the code is executed "INSIDE the http get" is invoked as the last debug log. I get the results from the get call but since its returned later, I am unable to do any processing with it. This is the exactl reason why we promises right? I need the promised data to do some processing inside the controller.

Any issue in my promise implementation??

Sanath
  • 4,774
  • 10
  • 51
  • 81
  • It should be `$scope.mydata = data.data;`. You dont need to do `$scope.$apply()` – AlwaysALearner Sep 05 '13 at 07:31
  • Also, `then` is executed after the response is received. So you can move your `after the http get` part inside the `then` function. – AlwaysALearner Sep 05 '13 at 07:34
  • ideally I would like to obtain the response and then process it in the controller rather than move the logic inside the then function.BTW the main point of promise is facilitating asnyc calls so ideally , it has to wait until all data in the http call is loaded and then continue with th rest of the page? am I right? – Sanath Sep 05 '13 at 07:53

1 Answers1

1

I'm not sure whether I get your question, but it looks like you've built a promise interseptor, but from your question it looks like you want just the regular promise behaviour. So I'll try that..

I'm not an angular expert but I do use $http promises a lot and here's how I do it.

I register the $http call as a service, like this:

app.service('ajax',function(host,$http){
  this.post = function(api,data,cb){
    $http.post(host + api,data).success(cb)
  };
  this.get = function(api,cb){
    $http.get(host + api).success(cb)
  }
});

host is a predefined module.value. I inject this service into every controller that needs to call http requests and operate them like this:

ajax.post('users', UserToken, function(data){
  console.log('Users points: ' + data.points)
})

As I understand $http has promises built in, so there's no need for q and defere, it's all done at the background. ajax.post calls the service, which sends data to host + 'users', server-side looks for user by his token and return some data with one key by the name of points with a value of the users points. Client-side: upon successful reply from the server it proceeds to the cb function which then console logs the user points.

So I can do whatever modification I need to the promise inside that cb function, since it is not called before a successful reply from the server.

The success and error methods have a few more optional arguments, check them out here.

Stav Geffen
  • 300
  • 3
  • 12