0

I am using AngularJS and its $q.

I wrote a service as follows:

Get.$inject = ['$q', '$http'];
function Get($q, $http) {
    return {
        get: function(url) {
            var deferred = $q.defer();

            $http.get(url + '?' + Math.random()).success(function(data) {
                alert('Got data: ' + JSON.stringify(data));
                deferred.resolve(data);
            });

            return deferred.promise;
        }
    }
}

and used it like

$q.all(Get.get('backend/index.php/poster/pull'), Get.get('data/poster_past.json')).then(
                function(NewAndOldPosters) {
                    alert("Old and New: " + JSON.stringify(NewAndOldPosters));
                }
    );

But I received an alert as "Old and New: {"$$state":{"status":0}}" before I received the alert of the actual data "Got Data: ......". How can this happen that $q.all does not work as expected?

Colliot
  • 1,522
  • 3
  • 16
  • 29

1 Answers1

2

Wrap your promises in an array:

[Get.get('backend/index.php/poster/pull'),Get.get('data/poster_past.json')]
Brett DeWoody
  • 59,771
  • 29
  • 135
  • 184