1

I'm developing an AngularJS application.

I try to write a service method that can periodically auto reload some content from HTTP request and notify the controller that called the method.

So I search to use the promse API of AngularJS and I use the $timeout service, but I can't return the promise while the method is running not asynchronously...

So here, the promise is never returned, and I understand why, but I don't know what is the correct pattern to do what I want.

angular.module('myModule')

.factory('myService', [
    '$timeout',
    '$q',
    '$doMyHttpRequest',
function($timeout, $q, $doMyHttpRequest){

    var d = $q.defer();

    return function(){        
        autoRefresh = function(){

            $timeout(function(){
                $doMyHttpRequest(function(){
                    d.notify({ // notify the update success
                        updated: true
                    });
                },function(){
                    d.notify({ // notify the update error
                        updated: false
                    });             
                });
                this.autoRefresh(); // refresh again in 60 seconds
            },60000);

        };
        this.autoRefresh();  

        return d.promise;
    }

}])

.controller('myCtrl', ['myService', function(myService){

    myService().autoRefresh().then(function(){ // never reached because promise is never returned
        // do my stuff when succeed
    },function(){ // never reached because promise is never returned
        // do my stuff when error
    });

}])

;
4
  • try d.resolve() / d.reject() instead of d.notify() Commented Aug 5, 2014 at 12:59
  • this can't resolve anything since the problem is that the promise is not (never) returned while the return statement is never reached in the function. Then, resolve(), reject() or notify() is not the point. Commented Aug 5, 2014 at 13:02
  • maybe put the code of autoReloadObservations() ? notice too that autoRefresh is never called in ur example Commented Aug 5, 2014 at 13:05
  • I am sorry I refactored my code for the needs of the example and forgotten renaming functions. Please consider my corrections. Commented Aug 5, 2014 at 13:14

1 Answer 1

2

please see here :http://jsbin.com/wiveli/1/edit

app.controller('firstCtrl', function($timeout,  doMyHttpRequest){

        function onSucess() {

              console.log("sucess");
               autoRefresh();

            };
         function onError() {

              console.log("error");
              autoRefresh();

            }


    function autoRefresh(){




        $timeout(function(){
         doMyHttpRequest.getData().then(onSucess, onError);

        },1000);  

    }
   autoRefresh();

});
Sign up to request clarification or add additional context in comments.

1 Comment

It seems the tip I searched for. I will try your solution then come back to valid it. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.