Worth noting that $http.get returns a promise. So you can do

function getData() {
  return $http.get().then(function(result){do stuff with result, return
data})
}

getData().then(function (data) {other stuff}).catch(function(err){bad
stuff})

instead of mucking around with $q at all. The .error function on
$http.get() will just run out to a .catch on the end of the chain (or you
could put one in earlier), and I'm 99% sure that angular at least
console-logs stuff that would go to .error or .catch.

If you're waiting on two promises to resolve and they can go in either
order, you can do $q.all([prom1, prom2, prom3]).then(function(array){
array[0] == prom1's resolve value, etc

I often use Array.map in this situation.
$q.all(paramArray.map(function(item){return
functionThatReturnsApromise(item)})) sort of thing.

If you're doing them sequentially p1 has to precede p2 as p2 relies on p1,
then you can just run them in then blocks.  p1.then(f(d){return
p2}).then(f(stuff){both done}. When I have a series like this, I put them
in an array as before, but use Array.reduce on them.

e



On Wed, Aug 27, 2014 at 9:51 AM, Darren D'Orlando <[email protected]
> wrote:

> This approach seems to work really well, when you have to call multiple
> rest services to get the data to populate a page.  In other words, if I
> want to call two web services, but I don't want to show data until both
> responses come back.
>
>
> On Wednesday, September 12, 2012 10:53:33 AM UTC-6, Uri Goldshtein wrote:
>
>> Hi,
>>
>>    I've created a service that calls $http (not $resource) and to get the
>> value to the controller.
>>    It took some time and i found a lot of example but not exactly what i
>> needed.
>>    Maybe this will help someone:
>>
>>    The service:
>>
>> angular.module('myApp.services', []).
>>     service('Activities', function($http, $q) {
>>         this.get = function(from, to){
>>             var deferred = $q.defer();
>>             var url = 'user/activities?from='+from+'&to='+to;
>>             $http.get(url).success(function(data, status) {
>>                 // Some extra manipulation on data if you want...
>>                 deferred.resolve(data);
>>             }).error(function(data, status) {
>>                 deferred.reject(data);
>>             });
>>
>>             return deferred.promise;
>>         }
>>     }
>> );
>>
>>
>> The call inside the controller (don't forget to DI the service in the
>> controller's parameters):
>>
>>     var promise = Activities.get(now, monthAgo);
>>     promise.then(
>>         function(activities){$scope.transactions = activities;}
>>         ,function(reason){alert('Failed: ' + reason);}
>>      );
>>
>>
>>  --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/angular.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to