Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
350 views
in Technique[技术] by (71.8m points)

javascript - angularjs promise not resolving properly

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??

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...