1

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

  • 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 Apr 01 '17 at 20:59
  • 2
    Please provide a [mcve]. Also shouldn't need to use `$q.defer()` since `$http` itself returns a promise – charlietfl Apr 01 '17 at 20:59
  • 1
    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 Apr 01 '17 at 21:00
  • 1
    Avoid the [deferred antipattern](http://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it)! – Bergi Apr 01 '17 at 21:04
  • What is `pdata`? Is `promises` the same as `promisesList`? – Bergi Apr 01 '17 at 21:05
  • You haven't provided a sample jsbin or something we can test out here is a simple example of using q.all vs calling individual calls. http://jsbin.com/qeyataw/edit?js,output – Ace Apr 02 '17 at 04:19
  • pData is just an array which contains all of my objects that returned it's useless for me and ill drop it down, but it's not the issue, Does it possible that because im not using return to return the promise and adding it to array using the called function it causes a problem?, Maybe the way i pushing it to array causes the problem? – Yaron Ben Atar Apr 02 '17 at 15:06

1 Answers1

1

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.