1
votes

Im trying to use the $q.defer, $q.all and promises in order to wait for a list of http requests

i uses this code to call the $q.all

$q.all(promises).then(function(data) {
      console.log('All promises have resolved', data);

      var retVal = Utils.DoStep2();
      console.log(retVal);
});

this function is never called altough i checked and the $http.get is called for all of the values.

            var deferred = $q.defer(); 
           $http.get(requestUrl).
           then(function (data) {
               var p = {
                   data: data,
                   name: name
               };
               pData.push(p);
               deferred.resolve(p);
               return p;
           })
           .catch(function (status) {
               deferred.reject(status);
           });

           promisesList.push(deferred.promise);

Im printing on DoStep2 the length of pData and also the pData using console.log and what i get is the length of 0 and Looking like 0 objects but when i open it it looks like all of the objects are initalized within the $http.get call for each specific call which made me sure that the $http.get response recieved and it's a valid response.

Also the $all isn't called at all what could be wrong?

Thanks for your assistance

1
One possibility is that you're not getting success responses for every request. if you have a 400 response, that counts as a rejection to $http.get. Another is a straight runtime error in the .then() (I can't see pData declared, so .push could fail. - Stuart Watt
Please provide a minimal reproducible example. Also shouldn't need to use $q.defer() since $http itself returns a promise - charlietfl
Other issues: you push on promisesList but call .all on promises. Personally, I minimize use of $q.defer() -- the result of $http.get is a promise anyway, so you can push that directly. - Stuart Watt
What is pdata? Is promises the same as promisesList? - Bergi

1 Answers

1
votes

OK Managed to fix it up I used service function getService() and this function returned the promise then in each call i added the getService() promise returned to the promisesList this list i waited for using $all and it worked thanks alot for your assistance.