1

I am new to Angular, so this will be easy to most. I have a service with one function:

.factory('HttpHandler', function () {
    return {
        loadData: function (promise) {
            var self = {
                data: [],
                loading: true,
                promise: promise
            };

            promise.then(function (data) {
                self.data = data;
                self.loading = false;
            });

            promise.finally(function () {
                self.loading = false;
            });

            return self;
        }
    };
});

What I would like, is to be able to run something else after this has completed successfully. Seudo code:

var result = httpHandler.loadData(httpCall, function (complete) {
    console.log('this has completed');
});

Can someone explain to me how I can do this?

1
  • I wonder what you need this self object for at all. It seems to do not much more than the promise alone? Commented Mar 15, 2015 at 16:19

1 Answer 1

1

Without changing your httpHandler, this is the closest you can get to the behavior you describe in your code:

var resultPromise = httpHandler.loadData(httpCall)
                      .promise
                      .then(function (complete) {
                          console.log('this has completed.')
                      });

However, you probably want to save the result of httpHandler.loadData(httpCall) separately to use the other properties from therein:

var result = httpHandler.loadData(httpCall);
result.promise.then(function () {
    console.log('this has completed: ', result.loading, 
                ' with data: ', result.data);
});
Sign up to request clarification or add additional context in comments.

1 Comment

I knew it was simple :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.